Last active
March 25, 2016 02:53
-
-
Save tarasjg/0bef94e47fd6e1788a2a to your computer and use it in GitHub Desktop.
tic tac toe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Scanner; | |
| public class Exercise08_09 { | |
| public static void main(String[] args) { | |
| Scanner stdin = new Scanner(System.in); | |
| int[][] board = {{-1, -1, -1}, | |
| {-1, -1, -1}, | |
| {-1, -1, -1}}; | |
| int turn = 1; | |
| int player; | |
| while (winner(board) == 3) { | |
| printBoard(initializeBoard(board)); | |
| if (turn % 2 == 1) player = 0; | |
| else player = 1; | |
| int row; | |
| int column; | |
| do { | |
| row = getRow(stdin, player); | |
| column = getColumn(stdin, player); | |
| } while(!isValid(row, column, board)); | |
| updateBoard(board, row, column, player); | |
| turn++; | |
| } | |
| printBoard(initializeBoard(board)); | |
| if (winner(board) == 0) System.out.println("O wins!"); | |
| else if (winner(board) == 1) System.out.println("X wins!"); | |
| else if (winner(board) == -1) System.out.println("Draw!"); | |
| } | |
| //returns board as string | |
| public static String initializeBoard(int[][] board) { | |
| String bar = "-------------"; | |
| String xBlock = "| X "; | |
| String oBlock = "| O "; | |
| String bBlock = "| "; | |
| String master = ""; | |
| for (int i = 0; i < 3; i++) { | |
| master += bar + "\n"; | |
| for (int j = 0; j < 3; j++) { | |
| if (board[i][j] == -1) master += bBlock; | |
| else if (board[i][j] == 0) master += oBlock; | |
| else master += xBlock; | |
| } | |
| master += "|\n"; | |
| } | |
| master += bar; | |
| return master; | |
| } | |
| //prints board | |
| public static void printBoard(String board) { | |
| System.out.println(board); | |
| } | |
| //get row from user | |
| public static int getRow(Scanner stdin, int player) { | |
| String sPlayer; | |
| if (player == 0) sPlayer = "O"; | |
| else sPlayer = "X"; | |
| System.out.print("Enter a row, player " + sPlayer + " -> "); | |
| int row = stdin.nextInt(); | |
| return row; | |
| } | |
| //get column from user | |
| public static int getColumn(Scanner stdin, int player) { | |
| String sPlayer; | |
| if (player == 0) sPlayer = "O"; | |
| else sPlayer = "X"; | |
| System.out.print("Enter a column, player " + sPlayer + " -> "); | |
| int column = stdin.nextInt(); | |
| return column; | |
| } | |
| //validates row/column | |
| public static boolean isValid(int row, int column, int[][] board) { | |
| if ((row > 2 || row < 0) || (column > 2 || column < 0)) { | |
| return false; | |
| } else if (board[row][column] == 0 || board[row][column] == 1) { | |
| return false; | |
| } else { | |
| return true; | |
| } | |
| } | |
| //updates board | |
| public static void updateBoard(int[][] board, int row, int column, int player) { | |
| board[row][column] = player; | |
| } | |
| //checks for a winner or a tie | |
| public static int winner(int[][] board) { | |
| if ((board[0][0] == 0 && board[0][1] == 0 && board[0][2] == 0) || | |
| (board[1][0] == 0 && board[1][1] == 0 && board[1][2] == 0) || | |
| (board[2][0] == 0 && board[2][1] == 0 && board[2][2] == 0) || | |
| (board[0][0] == 0 && board[1][0] == 0 && board[2][0] == 0) || | |
| (board[0][1] == 0 && board[1][1] == 0 && board[2][1] == 0) || | |
| (board[0][2] == 0 && board[1][2] == 0 && board[2][2] == 0) || | |
| (board[0][0] == 0 && board[1][1] == 0 && board[2][2] == 0) || | |
| (board[2][0] == 0 && board[1][1] == 0 && board[0][2] == 0)) { | |
| return 0; // O wins | |
| } else if ((board[0][0] == 1 && board[0][1] == 1 && board[0][2] == 1) || | |
| (board[1][0] == 1 && board[1][1] == 1 && board[1][2] == 1) || | |
| (board[2][0] == 1 && board[2][1] == 1 && board[2][2] == 1) || | |
| (board[0][0] == 1 && board[1][0] == 1 && board[2][0] == 1) || | |
| (board[0][1] == 1 && board[1][1] == 1 && board[2][1] == 1) || | |
| (board[0][2] == 1 && board[1][2] == 1 && board[2][2] == 1) || | |
| (board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1) || | |
| (board[2][0] == 1 && board[1][1] == 1 && board[0][2] == 1)) { | |
| return 1; // X wins | |
| } | |
| for (int i = 0; i < 3; i++) { | |
| for (int j = 0; j < 3; j++) { | |
| if (board[i][j] == -1) { | |
| return 3; //continue | |
| } | |
| } | |
| } | |
| return -1; // tie | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment