-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeGame.java
More file actions
99 lines (87 loc) · 1.88 KB
/
Copy pathTicTacToeGame.java
File metadata and controls
99 lines (87 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.Scanner;
public class TicTacToeGame
{
private char[][] board;
public TicTacToeGame()
{
board = new char[3][3];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
}
public boolean makeMove(int row, int col, boolean isPlayerX)
{
if(board[row][col] == ' ')
{
if(isPlayerX)
{
board[row][col] = 'X';
}
else
{
board[row][col] = 'O';
}
return true;
}
else
return false;
}
public void printBoard()
{
System.out.println(board[0][0] + " | " + board[1][0] + " | " + board[2][0]);
System.out.println("- + - + -");
System.out.println(board[0][1] + " | " + board[1][1] + " | " + board[2][1]);
System.out.println("- + - + -");
System.out.println(board[0][2] + " | " + board[1][2] + " | " + board[2][2]);
/*for (int i = 0; i < 3; i++)
{
if (i != 0)
{
for (int k = 0; k < SIZE * 2 - 1; k++)
for (int j = 0; j < 3; j++)
{
if (j != 0)
System.out.print("|");
}
}*/
}
public static void main(String[] args)
{
String whosPlaying = "X";
boolean isXTurn = true;
int moveRow = 0, moveCol = 0, turnCount = 1;
TicTacToeGame game = new TicTacToeGame();
Scanner keyboard = new Scanner(System.in);
System.out.println("Who will go first?");
whosPlaying = keyboard.nextLine();
if (whosPlaying.equalsIgnoreCase("X"))
isXTurn = true;
else
isXTurn = false;
while (turnCount < 10)
{
System.out.println("Player " + whosPlaying + "'s turn!");
System.out.println("Which row?");
moveRow = keyboard.nextInt();
System.out.println("Which column?");
moveCol = keyboard.nextInt();
game.makeMove(moveRow, moveCol, isXTurn);
if (isXTurn)
{
isXTurn = false;
whosPlaying = "O";
}
else if (!isXTurn)
{
isXTurn = true;
whosPlaying = "X";
}
game.printBoard();
turnCount++;
}
}
}