import javax.swing.JOptionPane; import java.util.*; import java.io.*; import java.awt.*; /* * Code Name : Fill The Line * Written : Ahmad Jawed ZAFAR * Department : Computer Engineering * University : Bogazici University * Date : April 09, 2009 * */ public class GameTest { public char GameBoard[][]; public char sign1, sign2; public int maxLineLength; public GameTest() { sign1 = 'X'; sign2 = 'O'; maxLineLength = 4; GameBoard = new char[5][5]; } public void initializeBoard() { GameBoard[0][0] = ' '; GameBoard[0][1] = ' '; GameBoard[0][2] = ' '; GameBoard[0][3] = ' '; GameBoard[0][4] = ' '; GameBoard[1][0] = ' '; GameBoard[1][1] = ' '; GameBoard[1][2] = ' '; GameBoard[1][3] = ' '; GameBoard[1][4] = ' '; GameBoard[2][0] = ' '; GameBoard[2][1] = ' '; GameBoard[2][2] = ' '; GameBoard[2][3] = ' '; GameBoard[2][4] = ' '; GameBoard[3][0] = ' '; GameBoard[3][1] = ' '; GameBoard[3][2] = ' '; GameBoard[3][3] = ' '; GameBoard[3][4] = ' '; GameBoard[4][0] = ' '; GameBoard[4][1] = ' '; GameBoard[4][2] = ' '; GameBoard[4][3] = ' '; GameBoard[4][4] = ' '; } public void printBoard() { System.out.println(" -1---+--2--+--3--+--4--+--5--+"); System.out.println(" +-----+-----+-----+-----+-----+"); for (int i=0; i<5; i++) { System.out.print(i+1 + " -| "); for (int j=0; j<5; j++) { System.out.print(" " + GameBoard[i][j] + " | "); } System.out.println(); System.out.println(" +-----+-----+-----+-----+-----+"); } } public boolean putSign(String pos, char sign) { int x = 0, y = 0; x = (pos.charAt(0) - 48) - 1; y = (pos.charAt(1) - 48) - 1; if (GameBoard[y][x] != sign1 && GameBoard[y][x] != sign2) { GameBoard[y][x] = sign; return true; } else { return false; } } public boolean checkWinner(char sign) { int check=0, check2=0, check1=0, check3=0, check4=0, check5=0, check6=0, check7=0; for (int i=0; i<5; i++) { check = 0; check3 = 0; for (int j=0; j<5; j++) { if (GameBoard[i][j] == sign) { check++; if (check == maxLineLength) { return true; } } else { check=0; } if (GameBoard[j][i] == sign) { check3++; if (check3 == maxLineLength) { return true; } } else { check3 = 0; } } if (GameBoard[i][i] == sign) { check1++; if (check1 == maxLineLength) { return true; } } else { check1 = 0; } if (GameBoard[i][4-i] == sign) { check2++; if (check2 == maxLineLength) { return true; } } else { check2 = 0; } } ///////////////////////////// Second For ///////////////////// for (int k=0; k<4; k++) { if (GameBoard[k+1][k] == sign) { check4++; if (check4 == maxLineLength) { return true; } } else { check4 = 0; } if (GameBoard[k][k+1] == sign) { check5++; if (check5 == maxLineLength) { return true; } } else { check5 = 0; } /////////////////////// if (GameBoard[k][3-k] == sign) { check6++; if (check6 == maxLineLength) { return true; } } else { check6 = 0; } if (GameBoard[k+1][4-k] == sign) { check7++; if (check7 == maxLineLength) { return true; } } else { check7 = 0; } } return false; } public static void main(String[] args) { GameTest fl = new GameTest(); fl.initializeBoard(); fl.printBoard(); System.out.println(); boolean winner = false, checkForValidPos; // Begin The Game // int play1=0, play2=0, whoStart=0; String player, player1=null, player2=null, pos; int PlayOrExit=0, gamePlayed=0, maxLength, noWinner=0; Scanner scnr = new Scanner(System.in); System.out.println("Welcome To Fill A Line Game"); System.out.println("Enter Player 1 Name"); player1 = scnr.nextLine(); System.out.println("Enter Player 2 Name"); player2 = scnr.nextLine(); System.out.println("What Should Be The Max Line Length. (4) or (3)"); maxLength = scnr.nextInt(); if (maxLength == 4) { fl.maxLineLength = 4; System.out.println(fl.maxLineLength + " Is Assigned To Maximum Line Length"); } else if (maxLength == 3) { fl.maxLineLength = 3; System.out.println(fl.maxLineLength + " Is Assigned To Maximum Line Length"); } else { System.out.println("Invalid Input, Maximum Line Length Is '4' By Default"); } while (PlayOrExit != 2) { fl.printBoard(); System.out.println("Who Want To Start First. 1- "+player1 +"(X) , 2- " +player2 + "(O)"); whoStart = scnr.nextInt(); if (whoStart == 1) { player = player1; } else { player = player2; } while (winner != true) { if (noWinner == 25) { noWinner = 0; break; } if (player.equalsIgnoreCase(player1)) { System.out.println(player + " - Select A Position To Place Your Sign (X)"); pos = scnr.next(); checkForValidPos = fl.putSign(pos, 'X'); if (checkForValidPos == true) { player = player2; winner = fl.checkWinner('X'); noWinner++; } else { System.out.println("Invalid Position"); } } else if (player.equalsIgnoreCase(player2)) { System.out.println(player + " - Select A Position To Place Your Sign (O)"); pos = scnr.next(); checkForValidPos = fl.putSign(pos, 'O'); if (checkForValidPos == true) { player = player1; winner = fl.checkWinner('O'); noWinner++; } else { System.out.println("Invalid Position"); } } fl.printBoard(); } // Winner Decision // if (noWinner != 0) { if (player.equalsIgnoreCase(player2)) { System.out.println(player1 + " Has Won"); gamePlayed++; play1++; } else if (player.equalsIgnoreCase(player1)) { System.out.println(player2 + " Has Won"); gamePlayed++; play2++; } } else { System.out.println("Game Finished With Equality"); gamePlayed++; } System.out.println("To Play Again Enter 1, To Exit Enter 2"); PlayOrExit = scnr.nextInt(); if (PlayOrExit == 2) { System.out.println("Game Records :"); System.out.println("-------------------------------"); System.out.println("Game Played : " + gamePlayed + " Times"); System.out.println(player1 + " Has Won " + play1 + " Times"); System.out.println(player2 + " Has Won " + play2 + " Times"); System.out.println("-------------------------------"); System.out.println("Bye Bye"); System.exit(0); } else { System.console(); fl.initializeBoard(); noWinner=0; winner = false; } } } }