Repository navigation
Expand file tree
/
Copy pathTicTacToe.java
More file actions
157 lines (144 loc) · 5.3 KB
/
Copy pathTicTacToe.java
File metadata and controls
157 lines (144 loc) · 5.3 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* Eric Nguyen
* Tic-Tac-Toe Game
*/
import java.util.Scanner;
public class TicTacToe{
public static void main(String[] args) {
//initalizing scanner and boolean
Scanner input = new Scanner(System.in);
boolean valid = false;
int x = 5;
int y = 5;
// initalizing board
String [][] board = {
{"1","2","3"},
{"4","5","6"},
{"7","8","9"}};
do{
printBroad(board);
board = updateBoard(board, "o", input);
if(playerWon(board, "o") || tieGame(board)){
break;
}
printBroad(board);
board = updateBoard(board, "x", input);
if(playerWon(board, "x") || tieGame(board)){
valid = true;
}
}while(!valid);
}
//pre con: prompt user for int
// function validates the input
// post con: returns the valid int
public static int getInt(Scanner input) {
boolean good = false;
int number = 0;
do{
if(input.hasNextInt()){
number = input.nextInt();
input.nextLine();
if(number >= 1 && number <= 3) {
good =true;
}
else {
System.out.println("You entered an invalid number(Enter 1-3)");
}
}
else{
String temp = input.nextLine();
System.out.println(temp + " is not an integer");
}
}while(!good);
return number;
}
//preconditions: 2d array must be crated
//prints out the board
//post condition printined board
public static void printBroad(String[][] board){
for(String[] row:board){
for(String pos:row ){
System.out.print(pos + "\t");
}
System.out.println();
}
}
//pre-con: board was created and all rows and columns are initalized
// returns true if positions on board is being used
// false if it is not being used
// post con: returns true if pos is taken false if not.
public static boolean validPos(int col, int row, String[][] board){
String pos = board[row-1][col-1];
if(pos.equals("o") || pos.equals("x")){
System.out.println("This position is taken");
return true;
}
return false;
}
//pre-con: a 2d board snd scannermmust be initialized
// function updates baord by asking user where they want to place thier symbol
// and validates the position
// post-con: returns a 2d board with updates values coresspodnig nto user input.
public static String[][] updateBoard(String[][] board, String value, Scanner input){
boolean good = false;
int colPos = 0;
int rowPos= 0;
do{
System.out.print("Please enter the column you would like \""+ value +"\" in: ");
colPos = getInt(input);
System.out.print("\nPlease enter the row you would like \""+ value +"\" in: ");
rowPos = getInt(input);
//if position is not taken, end the loop
if (!(validPos(colPos, rowPos, board))){
board[rowPos-1][colPos-1] = value;
good = true;
}
}while(!good);
return board;
}
public static boolean tieGame(String[][] board){
for(String[] row:board) {
for(String pos:row){
if(!(pos.equals("o") || pos.equals("x"))){
return false;
}
}
}
System.out.print("Tie Game!");
return true;
}
//pre-con: board is created
// function determines winner
//post-con: returns true if someone won and returns false if no one won.;
public static boolean playerWon(String[][] board, String value) {
boolean hasWon = false;
if(board[0][0].equals(value) && board[0][1].equals(value) && board[0][2].equals(value)) {
hasWon = true;
}
else if(board[1][0].equals(value) && board[1][1].equals(value) && board[1][2].equals(value)) {
hasWon = true;
}
else if(board[2][0].equals(value) && board[2][1].equals(value) && board[2][2].equals(value)) {
hasWon = true;
}
else if (board[0][0].equals(value) && board[1][0].equals(value) && board[2][0].equals(value)) {
hasWon = true;
}
else if (board[0][1].equals(value) && board[1][1].equals(value) && board[2][1].equals(value)) {
hasWon = true;
}
else if (board[0][2].equals(value) && board[1][2].equals(value) && board[2][2].equals(value)) {
hasWon = true;
}
else if (board[0][0].equals(value) && board[1][1].equals(value) && board[2][2].equals(value)) {
hasWon = true;
}
else if (board[0][2].equals(value) && board[1][1].equals(value) && board[2][0].equals(value)) {
hasWon = true;
}
if(hasWon) {
System.out.println("player " + value + " won!");
}
return hasWon;
}
}