-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToeEditorWindow.cs
163 lines (139 loc) · 4.58 KB
/
TicTacToeEditorWindow.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
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: rimuru.dev@gmail.com
// - GitHub: https://github.com/RimuruDev
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub Organizations: https://github.com/Rimuru-Dev
//
// **************************************************************** //
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
namespace RimuruDev.Internal.Codebase.Editor
{
public sealed class TicTacToeEditorWindow : EditorWindow
{
private const string WindowName = "Tic Tac Toe";
private const string GameOverTitle = "Game Over :(";
private const string WinMessage = " Wins!";
private const string OK = "OK";
private const int BoardWidth = 100;
private const int BoardHeight = 100;
private readonly string[,] Board = new string[3, 3];
private bool isXTurn = true;
[MenuItem("RimuruDev Games/" + nameof(WindowName))]
public static void ShowWindow() =>
GetWindow<TicTacToeEditorWindow>(WindowName);
private void OnEnable() =>
ResetBoard();
private void OnGUI()
{
DrawBoards();
if (GUILayout.Button("Reset Board"))
ResetBoard();
}
private void DrawBoards()
{
for (var i = 0; i < 3; i++)
{
EditorGUILayout.BeginHorizontal();
{
for (var j = 0; j < 3; j++)
{
if (GUILayout.Button(Board[i, j], GUILayout.Width(BoardWidth), GUILayout.Height(BoardHeight)))
{
if (Board[i, j] == "")
{
Board[i, j] = isXTurn ? "X" : "O";
isXTurn = !isXTurn;
CheckForWinner();
}
}
}
}
EditorGUILayout.EndHorizontal();
}
}
private void ResetBoard()
{
for (var i = 0; i < 3; i++)
{
for (var j = 0; j < 3; j++)
{
Board[i, j] = "";
}
}
isXTurn = true;
}
private void CheckForWinner()
{
if (Board == null)
return;
CheckingLines();
CheckingColumns();
CheckingDiagonals();
CheckingTheDraw();
}
private void CheckingLines()
{
for (var i = 0; i < 3; i++)
{
if (Board[i, 0] == Board[i, 1] && Board[i, 1] == Board[i, 2] && Board[i, 0] != "")
{
EditorUtility.DisplayDialog(GameOverTitle, Board[i, 0] + WinMessage, OK);
ResetBoard();
return;
}
}
}
private void CheckingColumns()
{
for (var i = 0; i < 3; i++)
{
if (Board[0, i] == Board[1, i] && Board[1, i] == Board[2, i] && Board[0, i] != "")
{
EditorUtility.DisplayDialog(GameOverTitle, Board[0, i] + WinMessage, OK);
ResetBoard();
return;
}
}
}
private void CheckingDiagonals()
{
if (Board[0, 0] == Board[1, 1] && Board[1, 1] == Board[2, 2] && Board[0, 0] != "")
{
EditorUtility.DisplayDialog(GameOverTitle, Board[0, 0] + WinMessage, OK);
ResetBoard();
return;
}
if (Board[0, 2] == Board[1, 1] && Board[1, 1] == Board[2, 0] && Board[0, 2] != "")
{
EditorUtility.DisplayDialog(GameOverTitle, Board[0, 2] + WinMessage, OK);
ResetBoard();
}
}
private void CheckingTheDraw()
{
var isDraw = true;
for (var i = 0; i < 3; i++)
{
for (var j = 0; j < 3; j++)
{
if (Board[i, j] == "")
{
isDraw = false;
break;
}
}
}
if (isDraw)
{
EditorUtility.DisplayDialog(GameOverTitle, "It's a Draw!", OK);
ResetBoard();
}
}
}
}
#endif