/
Othellosystem.cs
415 lines (370 loc) · 11.5 KB
/
Othellosystem.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Othellosystem : MonoBehaviour
{
const int Field_Size_X = 8;
const int Field_Size_Y = 8;
public enum ePieceState
{
None,
Front,
Back,
}
private GameObject _BoardObject = null;
private GameObject [,] _FieldObject = new GameObject [Field_Size_X, Field_Size_Y];
private OthelloPiece [,] _Piece = new OthelloPiece [Field_Size_X, Field_Size_Y];
private ePieceState [,] _FieldPieceState = new ePieceState[Field_Size_X, Field_Size_Y];
private GameObject _CursorObject = null;
private GameObject _BackObject = null;
[SerializeField] GameObject _BoardPrefab = null;
[SerializeField] GameObject _PiecePrefab = null;
[SerializeField] GameObject _CursorPrefab = null;
[SerializeField] GameObject _XlinePrefab = null;
[SerializeField] GameObject _YlinePrefab = null;
[SerializeField] GameObject _BackPrefab = null;
private int _CursorX = 0;
private int _CursorY = 0;
private ePieceState _NowPiece = ePieceState.Back;
class Position
{
public int _x;
public int _y;
public Position(int x, int y)
{
_x = x;
_y = y;
}
}
// 処理の方向、上から時計回り
int[] Turn_Direction_X = new int[] {0, 1, 1, 1, 0, -1, -1, -1};
int[] Turn_Direction_Y = new int[] {1, 1, 0, -1, -1, -1, 0, 1};
public Text _Turn;
public Text _ScoreText;
public Text _GameRestart;
private const string WhiteTurn = "白のターン";
private const string BlackTurn = "黒のターン";
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < Field_Size_X; i++)
{
for (int j = 0; j < Field_Size_Y; j++)
{
GameObject NewObject = GameObject.Instantiate<GameObject>(_PiecePrefab);
OthelloPiece NewPiece = NewObject.GetComponent<OthelloPiece>();
NewObject.transform.localPosition = new Vector3(i - 3.5f, 0.0f, j - 3.5f);
_FieldObject[i, j] = NewObject;
_Piece[i, j] = NewPiece;
_FieldPieceState[i, j] = ePieceState.None;
}
_FieldPieceState[3, 3] = ePieceState.Back;
_FieldPieceState[4, 3] = ePieceState.Front;
_FieldPieceState[3, 4] = ePieceState.Front;
_FieldPieceState[4, 4] = ePieceState.Back;
}
_BoardObject = GameObject.Instantiate<GameObject>(_BoardPrefab);
_CursorObject = GameObject.Instantiate<GameObject>(_CursorPrefab);
_BackObject = GameObject.Instantiate<GameObject>(_BackPrefab);
// ラインの生成
for (int i = 0; i <= 8; i++)
{
GameObject temp = GameObject.Instantiate<GameObject>(_XlinePrefab);
LineRenderer XLine = temp.GetComponent<LineRenderer>();
XLine.SetPosition(0, new Vector3(-4.0f, 0.55f, i - 4.0f));
XLine.SetPosition(1, new Vector3(4.0f, 0.55f, i - 4.0f));
}
for (int j = 0; j <= 8; j++)
{
GameObject temp = GameObject.Instantiate<GameObject>(_YlinePrefab);
LineRenderer YLine = temp.GetComponent<LineRenderer>();
YLine.SetPosition(0, new Vector3(j - 4.0f, 0.51f, 4.0f));
YLine.SetPosition(1, new Vector3(j - 4.0f, 0.51f, -4.0f));
}
_Turn.text = BlackTurn;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
SceneManager.LoadScene("TitleScene");
}
int dX = 0;
int dY = 0;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
dY += 1;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
dY -= 1;
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
dX += 1;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
dX -= 1;
}
_CursorX += dX;
_CursorY += dY;
// オセロ盤から出ないように
if (_CursorX < 0)
{
_CursorX = 0;
}
else if (_CursorX > 7)
{
_CursorX = 7;
}
if (_CursorY < 0)
{
_CursorY = 0;
}
else if (_CursorY > 7)
{
_CursorY = 7;
}
_CursorObject.transform.localPosition = new Vector3(_CursorX - 3.5f, 0.5f, _CursorY - 3.5f);
if (Input.GetKeyDown(KeyCode.Return))
{
if (0 <= _CursorX && _CursorX < Field_Size_X && 0 <= _CursorY && _CursorY < Field_Size_Y && _FieldPieceState[_CursorX, _CursorY] == ePieceState.None && Turn(false) > 0)
{
_FieldPieceState[_CursorX, _CursorY] = _NowPiece;
Turn(true);
_NowPiece = ((_NowPiece == ePieceState.Back) ? ePieceState.Front : ePieceState.Back);
_Turn.text = ((_NowPiece == ePieceState.Back) ? BlackTurn : WhiteTurn);
}
}
// ピースの状態反映
UpdatePieceState();
// 全て埋まれば終了
UpdateFieldState();
// 置ける場所がないとパス、2が返る
PutCheck();
if (PutCheck() == 2)
{
_NowPiece = ((_NowPiece == ePieceState.Back) ? ePieceState.Front : ePieceState.Back);
_Turn.text = ((_NowPiece == ePieceState.Back) ? BlackTurn : WhiteTurn);
// 両方パスだと試合終了
PutCheck();
if (PutCheck() == 2)
{
GameOver();
}
}
}
int Turn(bool isTurn)
{
ePieceState AnotherColor = ((_NowPiece == ePieceState.Back) ? ePieceState.Front : ePieceState.Back);
bool Sandwich = false;
List<Position> PositionList = new List<Position>();
int count = 0;
for (int i = 0; i < 8; i ++)
{
int deltaX = 0, deltaY = 0;
int x = _CursorX;
int y = _CursorY;
deltaX += Turn_Direction_X[i];
deltaY += Turn_Direction_Y[i];
Sandwich = false;
PositionList.Clear();
while (true)
{
x += deltaX;
y += deltaY;
if (!(0 <= x && x < Field_Size_X && 0 <= y && y < Field_Size_Y))
{
break;
}
if (_FieldPieceState[x, y] == AnotherColor)
{
PositionList.Add(new Position(x, y));
}
else if (_FieldPieceState[x, y] == _NowPiece)
{
Sandwich = true;
break;
}
else
{
break;
}
}
// ひっくり返し処理
if (Sandwich)
{
count += PositionList.Count;
if (isTurn)
{
for (int j = 0; j < PositionList.Count; j++)
{
Position pos = PositionList[j];
_FieldPieceState[pos._x, pos._y] = _NowPiece;
_Piece[pos._x, pos._y].StartTurnAnimation();
}
}
}
}
return count;
}
//ピースの状態反映
void UpdatePieceState()
{
for (int i = 0; i < Field_Size_X; i++)
{
for(int j = 0; j < Field_Size_Y; j++)
{
_Piece[i, j].SetState(_FieldPieceState[i, j]);
}
}
}
// 全て埋まれば終了
void UpdateFieldState()
{
int NoneCount = 0;
for (int i = 0; i < Field_Size_X; i++)
{
for(int j = 0; j < Field_Size_Y; j++)
{
if (_FieldPieceState[i, j] == ePieceState.None)
{
NoneCount ++;
}
}
}
if (NoneCount == 0)
{
GameOver();
}
else
{
return;
}
}
// 試合終了処理
void GameOver()
{
int Black = BlackScore();
int White = WhiteScore();
_Turn.text = "ゲーム終了";
_GameRestart.text = "スペースキー最初から";
if (White > Black)
{
_ScoreText.text = "White win!!\r\n" + White + ":" + Black;
}
else if (Black > White)
{
_ScoreText.text = "Black win!!\r\n" + Black + ":" + White;
}
else
{
_ScoreText.text = "Draw!!\r\n" + White + ":" + Black;
}
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("Othello");
}
}
// 黒の集計
private int BlackScore()
{
int count = 0;
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
if (_FieldPieceState[x, y] == ePieceState.Back)
{
count++;
}
}
}
return count;
}
// 白の集計
private int WhiteScore()
{
int count = 0;
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
if (_FieldPieceState[x, y] == ePieceState.Front)
{
count++;
}
}
}
return count;
}
// 置ける場所の確認
int CanTurnStone(int Ax, int Ay)
{
ePieceState AnotherColor = ((_NowPiece == ePieceState.Back) ? ePieceState.Front : ePieceState.Back);
int CanTurnPiece = 0;
int CanPutCount = 0;
if (_FieldPieceState[Ax, Ay] == ePieceState.None)
{
for (int i = 0; i < 8; i ++)
{
int deltaX = 0, deltaY = 0;
int x = Ax;
int y = Ay;
deltaX += Turn_Direction_X[i];
deltaY += Turn_Direction_Y[i];
while (true)
{
x += deltaX;
y += deltaY;
if (!(0 <= x && x < Field_Size_X && 0 <= y && y < Field_Size_Y))
{
CanTurnPiece = 0;
break;
}
if (_FieldPieceState[x, y] == AnotherColor)
{
CanTurnPiece++;
}
else if (_FieldPieceState[x, y] == _NowPiece)
{
if (CanTurnPiece > 0)
{
CanPutCount++;
break;
}
else
{
break;
}
}
else
{
CanTurnPiece = 0;
break;
}
}
}
}
return CanPutCount;
}
// 置ける場所があると1を返す
int PutCheck()
{
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
if (CanTurnStone(x, y) > 0)
{
return 1;
}
}
}
return 2;
}
}