-
Notifications
You must be signed in to change notification settings - Fork 0
/
GamePieceScript.cs
174 lines (131 loc) · 6.06 KB
/
GamePieceScript.cs
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
174
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class GamePieceScript : MonoBehaviour
{
private CellStateScript cellState;
private UtilityScripts utility;
private ThreatCheckScript winConditions;
private DrawBoardScript drawBoard;
public int placedPieceX;
public int placedPieceY;
void Start()
{
utility = gameObject.GetComponent<UtilityScripts>();
winConditions = gameObject.GetComponent<ThreatCheckScript>();
drawBoard = gameObject.GetComponent<DrawBoardScript>();
}
//Get available empty spaces on board. Replaces getPossibleMoves in negamax algorithm
//use unmakeMove to undo the move after -negamax is called.
public List<BoardSpace> GetAvailableSpaces(int[,] gameBoard)
{
List<BoardSpace> spaces = new List<BoardSpace>();
for (int x = 0; x < gameBoard.GetLength(0); x++)
{
for (int y = 0; y < gameBoard.GetLength(1); y++)
{
//Catch IndexOutOfRange expection if player tries to place piece outside gameBoard
try
{
//Get gameState of current cell
int cellGameState = gameBoard[x, y];
//If current cell filled, set gamestate of previous cell to player or ai piece. Check for win. Advance turn
if (cellGameState == 0)
{
//available space
spaces.Add(new BoardSpace(x, y));
break;
}
//If column is full and new piece is placed, ignore it.
else if (y == 5 && (cellGameState == 1 || cellGameState == 2))
{
//Debug.Log("Column is full while getting spaces");
break;
}
}
//Log error if piece is placed outside game board
catch (Exception e)
{
Debug.LogException(e);
Debug.Log("Not legal move. Turn should not move on until legal move is made.");
}
}
}
return spaces;
}
//MakeMove for ai calculation in negamax
public void MakeMove(int[,] gameBoard, BoardSpace space, int player)
{
//Debug.Log("MakeMove space x, y: " + space.x + ", " + space.y + "\n");
//Debug.Log("gameBoard length: " + gameBoard.GetLength(0));
gameBoard[space.x, space.y] = player;
}
//Method to place piece. Includes calculation to check for empty space. Only requires x position
public void PlaceGamePiece(int[,] gameBoardStates, int cellX, bool virtualMove, int player)
{
//Set local variable PlayerTurn to value of global turn tracker
bool playerTurn = gameObject.GetComponent<GameController>().playerTurn;
int newGameState = player;
int winState = 0;
//Iterate down y axis of current column to check for empty cells
for (int y = gameBoardStates.GetLength(1)-1; y >= 0; y--)
{
//Catch IndexOutOfRange expection if player tries to place piece outside gameBoard
try
{
//Get gameState of current cell
int cellGameState = gameBoardStates[cellX, y];
//If current cell filled, set gamestate of previous cell to player or ai piece. Check for win. Advance turn
if ((cellGameState == 1 || cellGameState == 2) && y < 5)
{
//Isnt updating gameBoardState in drawBoardScript? check this
gameBoardStates[cellX, (y + 1)] = newGameState;
drawBoard.UpdateBoard(cellX, (y + 1));
placedPieceX = cellX;
placedPieceY = y + 1;
winState = winConditions.CheckForWin(gameBoardStates, newGameState, cellX, y+1);
//Only set winner if the move is an actual move, not an ai calculation
if (virtualMove == false)
{
gameObject.GetComponent<GameController>().winner = winState;
}
gameObject.GetComponent<GameController>().playerTurn = !playerTurn;
break;
}
//If column is full and new piece is placed, ignore it.
else if (y == 5 && (cellGameState == 1 || cellGameState == 2))
{
Debug.Log("Column is full");
break;
}
//If bottom of column reached then set gamestate of current cell. Check for win. Advance turn
else if (y == 0)
{
gameBoardStates[cellX, y] = newGameState;
drawBoard.UpdateBoard(cellX, (y));
placedPieceX = cellX;
placedPieceY = y;
winState = winConditions.CheckForWin(gameBoardStates, newGameState, cellX, y);
//Only set winner if the move is an actual move, not an ai calculation
if (virtualMove == false)
{
gameObject.GetComponent<GameController>().winner = winState;
}
gameObject.GetComponent<GameController>().playerTurn = !playerTurn;
break;
}
}
//Log error if piece is placed outside game board
catch (Exception e)
{
Debug.LogException(e);
Debug.Log("Not legal move. Turn should not move on until legal move is made.");
}
}
}
public void UnmakeMove(int[,] gameBoardStates, BoardSpace space)
{
gameBoardStates[space.x, space.y] = 0;
}
}