-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
103 lines (87 loc) · 2.25 KB
/
Copy pathMain.java
File metadata and controls
103 lines (87 loc) · 2.25 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
/**
* Class name: Main
* Main class for Tic Tac Toe project
*
* @author Christian Autor
* @version 1.0
* @since 3/30/2021
*/
import java.util.*;
public class Main
{
public static void main(String[] args)
{
MiniMax miniMax;
char playerChar;
char computerChar;
int turns;
Scanner sc = new Scanner(System.in);
playerChar = '-';
while(playerChar != 'O' && playerChar != 'X')
{
System.out.println("Chose 'X' or 'O' ");
playerChar = Character.toUpperCase(sc.next().charAt(0));
}
if(playerChar == 'O')
{
turns = 0;
miniMax = new MiniMax();
playerChar = 'O';
computerChar = 'X';
}
else
{
turns = 1;
miniMax = new MiniMax('X', 'O');
playerChar = 'X';
computerChar = 'O';
}
Board ticTacToeBoard = new Board();
System.out.print("\n\nInitial board state: ");
while(!miniMax.termTest(ticTacToeBoard))
{
ticTacToeBoard.printBoard();
if(turns%2 == 0)
{
int maxValue = -1;
int maxValuePosition = 0;
int tempValue;
for(int i = 0; i<9; i++)
{
if(ticTacToeBoard.getSpot(i) == '-')
{
tempValue = miniMax.miniMax(ticTacToeBoard.returnMove(computerChar,i), false);
if(tempValue>maxValue)
{
maxValue = tempValue;
maxValuePosition = i;
}
}
}
ticTacToeBoard.makeMove(computerChar,maxValuePosition);
System.out.print("\n\nComputer move: ");
}
else
{
System.out.print("\n");
int playerInput = -1;
while(playerInput < 1 || playerInput > 9 || ticTacToeBoard.getSpot(playerInput-1) != '-')
{
System.out.println("\nChose move (position 1-9) ");
playerInput = sc.nextInt();
}
ticTacToeBoard.makeMove(playerChar, playerInput-1);
System.out.print("\n\nYour move: ");
}
turns++;
}
ticTacToeBoard.printBoard();
miniMax.termTest(ticTacToeBoard);
if(miniMax.getValue() == playerChar)
System.out.println("\n\nYou Win!");
else if(miniMax.getValue() == computerChar)
System.out.println("\n\nComputer Wins");
else
System.out.println("\n\nTie");
}
}