Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chap7;
- import java.util.*;
- public class tictac {
- public static void main(String[] args) {
- public class TTTConsoleNonOO2P {
- int EMPTY = 0;
- int CROSS = 1;
- int OTHER = 2;
- int PLAYING = 0;
- int DRAW = 1;
- int CROSS_WON = 2;
- int OTHER_WON = 3;
- int ROWS = 3;
- int COLS = 3;
- int[][] board = new int[ROWS][COLS];
- int currentState;
- int currentPlayer;
- int currntRow, currentCol;
- public static Scanner in = new Scanner(System.in);
- public void main(String[] args) {
- initGame();
- do {
- playerMove(currentPlayer);
- updateGame(currentPlayer, currntRow, currentCol);
- printBoard();
- if (currentState == CROSS_WON) {
- System.out.println("'X' wins.");
- } else if (currentState == OTHER_WON) {
- System.out.println("'O' wins.");
- } else if (currentState == DRAW) {
- System.out.println("draw");}
- currentPlayer = (currentPlayer == CROSS) ? OTHER : CROSS;}
- while (currentState == PLAYING);}
- public void initGame() {
- for (int row = 0; row < ROWS; ++row) {
- for (int col = 0; col < COLS; ++col) {
- board[row][col] = EMPTY;
- }
- }
- currentState = PLAYING;
- currentPlayer = CROSS;
- }
- public void playerMove(int theSeed) {
- boolean validInput = false;
- do {
- if (theSeed == CROSS) {
- System.out.print("Player 'X', enter your move (row[1-3] column[1-3]): ");
- } else {
- System.out.print("Player 'O', enter your move (row[1-3] column[1-3]): ");
- }
- int row = in.nextInt() - 1; // array index starts at 0 instead of 1
- int col = in.nextInt() - 1;
- if (row >= 0 && row < ROWS && col >= 0 && col < COLS && board[row][col] == EMPTY) {
- currntRow = row;
- currentCol = col;
- board[currntRow][currentCol] = theSeed;
- validInput = true;
- } else {
- System.out.println("This move at (" + (row + 1) + "," + (col + 1)
- + ") is not valid. Try again...");
- }
- } while (!validInput);
- }
- public void updateGame(int theSeed, int currentRow, int currentCol) {
- if (hasWon(theSeed, currentRow, currentCol)) {
- currentState = (theSeed == CROSS) ? CROSS_WON : OTHER_WON;
- } else if (isDraw()) {
- currentState = DRAW;
- }
- }
- public boolean isDraw() {
- for (int row = 0; row < ROWS; ++row) {
- for (int col = 0; col < COLS; ++col) {
- if (board[row][col] == EMPTY) {
- return false;
- }
- }
- }
- return true;
- }
- public boolean hasWon(int theSeed, int currentRow, int currentCol) {
- return (board[currentRow][0] == theSeed
- && board[currentRow][1] == theSeed
- && board[currentRow][2] == theSeed
- || board[0][currentCol] == theSeed
- && board[1][currentCol] == theSeed
- && board[2][currentCol] == theSeed
- || currentRow == currentCol
- && board[0][0] == theSeed
- && board[1][1] == theSeed
- && board[2][2] == theSeed
- || currentRow + currentCol == 2
- && board[0][2] == theSeed
- && board[1][1] == theSeed
- && board[2][0] == theSeed);}
- public void printBoard() {
- for (int row = 0; row < ROWS; ++row) {
- for (int col = 0; col < COLS; ++col) {
- printCell(board[row][col]);
- if (col != COLS - 1) {
- System.out.print("|");
- }
- }
- System.out.println();
- if (row != ROWS - 1) {
- System.out.println("-----------");
- }
- }
- System.out.println();
- }
- public void printCell(int content) {
- switch (content) {
- case EMPTY: System.out.print(" "); break;
- case OTHER: System.out.print(" O "); break;
- case CROSS: System.out.print(" X "); break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement