-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewBehaviourScript.cs
147 lines (132 loc) · 3.53 KB
/
NewBehaviourScript.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
nowSize = 1;
ifFinish = false;
ifPeace = false;
}
// Update is called once per frame
void Update () {
}
void setChess(int row , int col){
chessBoard [row, col] = nowSize;
checkFinish ();
if(ifFinish){
return ;
}
else if (nowSize == 1) {
nowSize = 2;
}
else {
nowSize = 1;
}
}
void resetChessBoard(){
for (int loop = 0; loop < 3; loop++) {
for (int loop1 = 0; loop1 < 3; loop1++) {
chessBoard [loop, loop1] = 0;
}
}
ifFinish = false;
ifPeace = false;
}
void checkWin(){
for (int loop = 0; loop < 3; loop++) {
if (chessBoard [loop, 0] != 0 && chessBoard [loop, 0] == chessBoard [loop, 1]
&& chessBoard [loop, 0] == chessBoard [loop, 2]) {
ifFinish = true;
}
if (chessBoard [0, loop] != 0 && chessBoard [0, loop] == chessBoard [1, loop]
&& chessBoard [0, loop] == chessBoard [2, loop]) {
ifFinish = true;
}
}
if (chessBoard [0, 0] != 0 && chessBoard [0, 0] == chessBoard [1, 1]
&& chessBoard [0, 0] == chessBoard [2, 2]) {
ifFinish = true;
}
if (chessBoard [0, 2] != 0 && chessBoard [0, 2] == chessBoard [1, 1]
&& chessBoard [0, 2] == chessBoard [2, 0]) {
ifFinish = true;
}
}
void checkPeace(){
ifPeace = true;
for (int loop = 0; loop < 3; loop++) {
for (int loop1 = 0; loop1 < 3; loop1++) {
if(chessBoard[loop , loop1] == 0){
ifPeace = false;
}
}
}
}
void checkFinish(){
checkWin ();
if (!ifFinish) {
checkPeace ();
}
if (ifPeace) {
ifFinish = true;
}
else if (ifFinish) {
winCount [nowSize - 1]++;
}
}
void OnGUI () {
// Make a background box
if(ifFinish){
string str;
GUIStyle winBoxStyle = new GUIStyle(GUI.skin.box);
winBoxStyle.fontSize = 30;
if (ifPeace) {
str = "Peace !!!";
}
else if (nowSize == 1) {
str = "√ win !!!";
}
else{
str = "x win !!!";
}
GUI.Box( new Rect(250 , 150 , 200, 100) , str , winBoxStyle);
if( GUI.Button( new Rect(335 , 210 , 30, 30) , "ok" ) ){
resetChessBoard ();
}
return ;
}
GUI.Box(new Rect(10,10,100,60), "Menu");
GUI.Box(new Rect(10,80,100,90), "scorecard");
GUI.Label (new Rect (30, 100, 60, 20), "√ :" + winCount[0].ToString() );
GUI.Label (new Rect (30, 120, 60, 20), "× :" + winCount[1].ToString() );
GUI.Box(new Rect(200,10,350,350) , "");
GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
buttonStyle.fontSize = 30;
for (int loop = 0; loop < 3; loop++) {
for (int loop1 = 0; loop1 < 3; loop1++) {
switch(chessBoard[loop , loop1]){
case 1:
GUI.Button (new Rect (220 + loop1 * 110, 20 + loop * 110, 100, 100), "√" , buttonStyle);
break;
case 2:
GUI.Button (new Rect (220 + loop1 * 110, 20 + loop * 110, 100, 100), "×" , buttonStyle);
break;
default:
if(GUI.Button (new Rect (220 + loop1 * 110, 20 + loop * 110, 100, 100), "", buttonStyle)){
setChess (loop, loop1);
}
break;
}
}
}
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if(GUI.Button(new Rect(20,40,80,20), "reset")) {
resetChessBoard ();
}
}
private int[,] chessBoard = new int[3 , 3];
private int nowSize;
private bool ifFinish , ifPeace;
private int[] winCount = new int [2];
}