-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.java
62 lines (57 loc) · 2.56 KB
/
main.java
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
import java.util.*;
public class TicTacToeBoardFace0 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
char player = 'X';
int rowNum = -1, colNum = -1;
//making board
char[][] board = new char[3][3];
//initialize
for(int row = 0; row < board.length; row++){
for(int col = 0; col < board[row].length; col++){
board[row][col] = '-';
}
}
do { //game keeps running until someone wins
System.out.println("Printing the board info....");
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.print(board[row][col] + " ");
}
System.out.println();
}
//player selects the row to place their piece
System.out.print("Player" + player + ", please select a row number (0-2): ");
rowNum = input.nextInt();
//player selects the column to place their piece
System.out.print("Player" + player + ", please select a column number (0-2): ");
colNum = input.nextInt();
System.out.println();
//if it is valid, make move, update board, show the board
if (board[rowNum][colNum] == '-') {
board[rowNum][colNum] = player;
//check if we have winner
if((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][0] == player && board[0][1] == player && board[0][2] == player) ||
(board[1][0] == player && board[1][1] == player && board[1][2] == player) ||
(board[2][0] == player && board[2][1] == player && board[2][2] == player)){
System.out.println("Printing the board info....");
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
System.out.print(board[row][row] + " ");
}
System.out.println();
}
//display when the player win
System.out.println(player + " Won!");
break;
}
else{
//both players take turns
if(player == 'X') player = 'O';
else player = 'X';
}
}
}while(true);
}
}