-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtictac.java
More file actions
75 lines (50 loc) · 1.8 KB
/
tictac.java
File metadata and controls
75 lines (50 loc) · 1.8 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
//
//This will be the main program for running the game in the terminal.
//
import java.util.Random;
public class tictac {
private static void main() {
//initialize and assign x and o to player.
System.out.println("Please enter your name, player one.");
String name1 = TextIO.getln();
Player player1 = new Player(name1);
System.out.println("Please enter your name, player two.");
String name2 = TextIO.getln();
Player player2 = new Player(name2);
Random rand = new Random();
int n = rand.nextInt(10) + 1;
if (n < 5) {
player1.x = true;
player2.o = true;
} else {
player1.o = true;
player2.x = true;
}
//initialize board
Board board = new Board();
//accept inputs and print the current board, check if win or tie each turn
if (board.winner(board.playerboard) == null || board.tie(board.playerboard) == false) {
String[][] printboard = board.print(board);
for (int i = 0; i < printboard.length; i++) {
for (int j = 0; j < printboard.length ; j++) {
System.out.print(printboard[i][j]);
}
}
} else if (board.winner(board.playerboard) != null) {
System.out.println(board.winner(board.playerboard) + " has won!");
board.winner(board.playerboard).addScore();
} else if (board.tie(board.playerboard) == false) {
System.out.println("Tie!");
} else {
System.out.println("Oops. Something happened that shouldn't have happened.");
}
//if win or tie, end the game and declare the winner, or if there was a tie.
//prompt to restart program.
System.out.println(player1.getName() + ": " + player1.getScore()
+ " " + player2.getName() + ": " + player2.getScore());
System.out.println("Play Again? Y/N");
char play = TextIO.getChar();
if (play == 'y' || play == 'Y') {
}
}
}