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
| using UnityEngine;
public class Main :MonoBehaviour { private int[,] check = new int[3, 3]; private int[,] map = new int[3, 3]; public int turn = 1, count = 0, win = 0;
void Start() { turn = 1; count = 0; win = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { check[i, j] = 0; map[i, j] = 0; } }
private void OnGUI() { if (GUI.Button(new Rect(900, 650, 100, 60), "Reset")) Start();
GUIStyle style = new GUIStyle(); style.fontSize = 30; style.alignment = TextAnchor.MiddleCenter; style.fontStyle = FontStyle.BoldAndItalic; style.normal.textColor = Color.red;
if (win == 1) GUI.Label(new Rect(900, 180, 100, 100), "Player1 WIN", style); else if (win == 2) GUI.Label(new Rect(900, 180, 100, 100), "Player2 WIN", style); else if (win == 3) GUI.Label(new Rect(900, 180, 100, 100), "TIE", style);
for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { if (map[i, j] == 1) GUI.Button(new Rect(i * 100 + 800, j * 100 + 300, 100, 100), "O", style: style); else if (map[i, j] == -1) GUI.Button(new Rect(i * 100 + 800, j * 100 + 300, 100, 100), "X", style: style); if (GUI.Button(new Rect(i * 100 + 800, j * 100 + 300, 100, 100), "")) { if (win > 0) return; if (System.Math.Abs((check[0, i] += turn)) == 3) win = turn > 0 ? 1 : 2; if (System.Math.Abs((check[1, j] += turn)) == 3) win = turn > 0 ? 1 : 2; if (i == j && System.Math.Abs((check[2, 0] += turn)) == 3) win = turn > 0 ? 1 : 2; if (i + j == 2 && System.Math.Abs((check[2, 1] += turn)) == 3) win = turn > 0 ? 1 : 2; if (win == 0 && ++count == 9) win = 3; map[i, j] = turn; turn = -turn; } } } } }
|