-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompAI.java
More file actions
173 lines (157 loc) · 6.15 KB
/
Copy pathCompAI.java
File metadata and controls
173 lines (157 loc) · 6.15 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.example.itay.sumika;
import android.graphics.Point;
import android.widget.Button;
import java.util.Arrays;
import java.util.Random;
/**
* Created by itay on 23/11/17.
*/
public class CompAI extends Player {
boolean[][] available;
GameLogic gmLogic = new GameLogic();
public CompAI(int[][] cells, Player player) {
super(player);
available = new boolean[cells[0].length][cells.length];
for (int rows = 0; rows < cells[0].length; rows++) {
for (int columns = 0; columns < cells.length; columns++)
available[rows][columns] = (cells[rows][columns] == 0);
}
}
//old function, for only random
public void play(GameManager gm, int[][] cells, Point lastChoice, int validSpace) {
if (!gm.checkEndOfTheGame(cells, lastChoice, validSpace)) {
if (gm.getUserClicks == false) {
Point p = chooseRandomMove(cells, lastChoice, validSpace);
gm.game(p);
}
} else {
gm.announceWinner();
}
}
public Point chooseRandomMove(int[][] cells, Point lastChoice, int validSpace) {
Random rnd = new Random();
int x, y;
fillWithTrue(available);
Point p = new Point(-100, -100);//initialize initial values to know if no point was found
int rows = available.length;
int columns = available[0].length;
boolean foundSolution = false;
//while (!foundSolution&& isThereAvailableMoves(available)){
while (!foundSolution) {
x = rnd.nextInt(columns);
y = rnd.nextInt(rows);
p.set(x, y);
if (gmLogic.isValid(cells, p, lastChoice, validSpace)||!isThereAvailableMoves(available)) {
available[x][y] = false;
foundSolution = true;
}
}
return p;
}
//with minimax
public void test(GameManager gm, Player player2, int compLevel, int cells[][], Point lastChoice, int validSpace) {
if (!gm.checkEndOfTheGame(cells, lastChoice, validSpace)) {
if (gm.getUserClicks == false) {
MiniMaxAI miniMaxAI = new MiniMaxAI(player2.getNumOfPlayer(), cells, 4, lastChoice, gm.getTurn(), validSpace);
Point p = miniMaxAI.callMiniMax();
if (miniMaxAI.pointNull) {
p = chooseRandomMove(cells, lastChoice, validSpace);
}
gm.game(p);
} else {
gm.announceWinner();
}
}
}
public void test2(GameManager gm, Player player2, int compLevel, int cells[][], Point lastChoice, int validSpace) {
if (!gm.checkEndOfTheGame(cells, lastChoice, validSpace)) {
Point p;
if (gm.getUserClicks == false) {
switch (compLevel) {
case (1):
p = easy(cells, lastChoice, validSpace);
gm.game(p);
break;
case (2):
p = chooseRandomMove(cells, lastChoice, validSpace);
gm.game(p);
break;
case (3):
p = hard(cells, lastChoice, validSpace);
gm.game(p);
break;
}
}
} else {
gm.announceWinner();
}
}
public Point easy(int[][] cells, Point lastChoice, int validSpace) {
boolean foundGoodSolution=false;
fillWithTrue(available);
Random rnd = new Random();
int x, y;
Point p = new Point(-100, -100);//initialize initial values to know if no point was found
int rows = available.length;
int columns = available[0].length;
boolean foundSolution = false;
while (!foundSolution) {
x = rnd.nextInt(columns);
y = rnd.nextInt(rows);
p.set(x, y);
available[p.x][p.y]=false;
if(GameLogic.isValid(cells,p,lastChoice,validSpace)){
int[][] modifiCells=GameLogic.deepCopyIntMatrix(cells);
foundGoodSolution=!GameLogic.checkIfWiningMove(p, modifiCells, validSpace);
}
if (foundGoodSolution||!isThereAvailableMoves(available)) {
foundSolution = true;
}
}
if (!foundGoodSolution)
p = chooseRandomMove(cells, lastChoice, validSpace);
return p;
}
public Point hard(int[][] cells, Point lastChoice, int validSpace) {
boolean foundGoodSolution=false;
fillWithTrue(available);
Random rnd = new Random();
int x, y;
Point p = new Point(-100, -100);//initialize initial values to know if no point was found
int rows = available.length;
int columns = available[0].length;
boolean foundSolution = false;
while (!foundSolution){
x = rnd.nextInt(columns);
y = rnd.nextInt(rows);
p.set(x, y);
available[p.x][p.y]=false;
if(GameLogic.isValid(cells,p,lastChoice,validSpace)){
int[][] modifiCells=GameLogic.deepCopyIntMatrix(cells);
foundGoodSolution=GameLogic.checkIfWiningMove(p, modifiCells, validSpace);
}
if (foundGoodSolution||!isThereAvailableMoves(available))
foundSolution = true;
}
if (!foundGoodSolution)
p = chooseRandomMove(cells, lastChoice, validSpace);
return p;
}
private boolean isThereAvailableMoves(boolean[][] available) {
boolean isAvailable = false;
for (int rows = 0; rows < available[0].length; rows++) {
for (int columns = 0; columns < available.length && !isAvailable; columns++) {
if (available[rows][columns])
isAvailable = true;
}
}
return isAvailable;
}
private void fillWithTrue(boolean[][] available){
for (int rows = 0; rows < available[0].length; rows++) {
for (int columns = 0; columns < available.length;columns++) {
available[rows][columns]=true;
}
}
}
}