-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardManager.cs
More file actions
227 lines (191 loc) · 6.98 KB
/
BoardManager.cs
File metadata and controls
227 lines (191 loc) · 6.98 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using ConnectFourGame;
using TMPro;
public class BoardManager : MonoBehaviour
{
[Header("Prefabs")]
public GameObject boardPrefab; // Assign in Unity Editor
public GameObject floorPrefab; // Assign in Unity Editor
public GameObject redCoinPrefab; // Assign in Unity Editor
public GameObject yellowCoinPrefab; // Assign in Unity Editor
[Header("UI")]
public TMP_Text messageText; // Assign a UI Text for messages (optional)
public Button[] columnButtons; // Assign 7 UI buttons, one for each column
public Button resetButton; // Assign reset button
//public Dropdown dropdown; //dropdown for difficulty
private GameObject boardObject;
private GameObject floorObject;
// Store coin GameObjects for each cell
private GameObject[,] coins = new GameObject[6, 7];
private Board board = new Board();
private int currentPlayerId = 1; // 1: Yellow, 2: Red
// Coin drop positions (center of each column above the board)
private Vector3[] columnDropPositions = new Vector3[7];
private bool gameOver = false;
private float stationaryTime = 0f;
private int dificulty = 7;
private string player1Name = "Opponnent";
private string player2Name = "Ramy";
void Start()
{
// Instantiate board and floor at proper positions
boardObject = boardPrefab;
floorObject = floorPrefab;
// Set up drop positions for coins (adjust values for your scene/board size)
float startX = 36f; // Left-most column center
float stepX = 5.7f; // Distance between columns
float dropY = 130f; // Drop height above board
for (int i = 0; i < 7; i++)
{
columnDropPositions[i] = new Vector3(startX + i * stepX, dropY, 100f);
}
// Hook up button events
for (int col = 0; col < columnButtons.Length; col++)
{
int capturedCol = col; // Local copy for delegate
columnButtons[col].onClick.AddListener(() => OnColumnButtonClicked(capturedCol));
}
resetButton.onClick.AddListener(() => ResetBoard());
//changeDifficulty(dropdown.value + 1);
ShowMessage(player1Name+"'s turn!",Color.yellow);
}
void changeDifficulty(int diff)
{
dificulty = diff;
Debug.Log(diff);
}
void Update()
{
if (gameOver )
{
return;
}
if (currentPlayerId == 2)
{
stationaryTime += Time.deltaTime;
if (stationaryTime >=2f)
{
int col = Player.MakeMove(ref board, dificulty);
int row = board.Drop(currentPlayerId, col);
// Instantiate the correct coin prefab and let physics handle the drop
GameObject coinPrefab = (currentPlayerId == 1) ? yellowCoinPrefab : redCoinPrefab;
Vector3 dropPosition = columnDropPositions[col];
GameObject coinObject = Instantiate(coinPrefab, dropPosition, Quaternion.identity);
coins[row, col] = coinObject;
// Win/draw check after move
if (board.IsWin(row, col))
{
bool isYellow = currentPlayerId == 1;
ShowMessage((isYellow ? player1Name : player2Name) + " wins!", isYellow ? Color.yellow : Color.red);
DisableAllColumnButtons();
gameOver = true;
return;
}
else if (board.IsDraw())
{
ShowMessage("Draw!");
DisableAllColumnButtons();
gameOver = true;
return;
}
// Switch player
currentPlayerId = (currentPlayerId == 1) ? 2 : 1;
ShowMessage(
currentPlayerId == 1 ? player1Name+"'s turn!" : player2Name+"'s turn!",
currentPlayerId == 1 ? Color.yellow : Color.red
);
}
}
else
{
stationaryTime = 0f; // Reset if moving
}
}
public void OnColumnButtonClicked(int col)
{
int row = board.Drop(currentPlayerId, col);
if (row == -1)
{
ShowMessage("Column full!");
return;
}
// Instantiate the correct coin prefab and let physics handle the drop
GameObject coinPrefab = (currentPlayerId == 1) ? yellowCoinPrefab : redCoinPrefab;
Vector3 dropPosition = columnDropPositions[col];
GameObject coinObject = Instantiate(coinPrefab, dropPosition, Quaternion.identity);
coins[row, col] = coinObject;
// Unity Physics handles coin falling and resting on floor/other coins.
// Make sure your coin prefabs have Rigidbody2D and BoxCollider2D.
// Floor prefab must have a static BoxCollider2D.
// Win/draw check after move
if (board.IsWin(row, col))
{
bool isYellow = currentPlayerId == 1;
ShowMessage((isYellow ? player1Name : player2Name) + " wins!", isYellow ? Color.yellow : Color.red);
DisableAllColumnButtons();
return;
}
else if (board.IsDraw())
{
ShowMessage("Draw!");
DisableAllColumnButtons();
return;
}
// Switch player
currentPlayerId = (currentPlayerId == 1) ? 2 : 1;
ShowMessage(
currentPlayerId == 1 ? player1Name+"'s turn!" : player2Name+"'s turn!",
currentPlayerId == 1 ? Color.yellow : Color.red
);
}
public void ResetBoard()
{
// Destroy all coin GameObjects
for (int row = 0; row < 6; row++)
for (int col = 0; col < 7; col++)
if (coins[row, col] != null)
{
Destroy(coins[row, col]);
coins[row, col] = null;
}
board.Reset();
currentPlayerId = (currentPlayerId == 1) ? 2 : 1;
ShowMessage(
currentPlayerId == 1 ? player1Name + "'s turn!" : player2Name + "'s turn!",
currentPlayerId == 1 ? Color.yellow : Color.red
);
EnableAllColumnButtons();
gameOver = false;
}
private void ShowMessage(string msg)
{
if (messageText != null)
messageText.text = msg;
else
Debug.Log(msg);
}
private void DisableAllColumnButtons()
{
foreach (var btn in columnButtons)
btn.interactable = false;
}
private void EnableAllColumnButtons()
{
foreach (var btn in columnButtons)
btn.interactable = true;
}
public void ShowMessage(string msg, Color color)
{
if (messageText != null)
{
messageText.text = msg;
messageText.color = color;
}
else
{
Debug.Log(msg);
}
}
}