2023년 11월 5일 일요일

C# - 틱텍토 게임

 이번엔 입력 출력함수로 틱텍토를 만들었다.

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
 
namespace tictactoe
{
    class Programs
    {
 
        static void Main(string[] args)
        {
 
            GameStart();
 
            return;
        }
 
 
        static void GameStart()
        {
            while (true)
            {
                char[,] fields = new char[77]
                { { '┌''―''┬''―''┬''―''┐'},
              { '│''ㅤ''│' ,'ㅤ''│''ㅤ''│'},
              { '├''―''┼' ,'―''┼''―''┤'},
              { '│''ㅤ''│' ,'ㅤ''│''ㅤ''│'},
              { '├''―''┼' ,'―''┼''―''┤'},
              { '│''ㅤ''│' ,'ㅤ''│''ㅤ''│'},
              { '└''―''┴''―''┴''―''┘'}
                 };
 
                char[,] check_field = new char[33];
                //그림 필드와 별개로 쉽게 승리체크하기 위해 새로운 배열로 계산
                int count = 1;
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        check_field[i, j] = (char)count;
                        count++;
                    }
                }
 
                count = 1;
                char stemp = '◆';
                bool whosTurn = true;
 
                while (true)
                {
                    Console.Clear();
 
                    //둘곳이 없으면 게임 재시작
                    if (count == 10)
                    {
                        break;
                    }
 
                    //바둑판 그리기
                    for (int i = 0; i < 7; i++)
                    {
                        for (int j = 0; j < 7; j++)
                        {
                            Console.Write(fields[i, j]);
                        }
 
                        Console.WriteLine();
                    }
 
                    //승리 체크
                    if (check_field[00== check_field[11&& check_field[22== check_field[11])
                    {
 
                        Console.WriteLine("승리!");
                        return;
                    }
                    //승리 체크
                    if (check_field[02== check_field[11&& check_field[20== check_field[11])
                    {
 
                        Console.WriteLine("승리!");
                        return;
                    }
                    //승리 체크
                    for (int b = 0; b < 3; b++)
                    {
 
                        if (check_field[0, b] == check_field[1, b] && check_field[1, b] == check_field[2, b])
                        {
                            Console.WriteLine("승리!");
                            return;
                        }
 
                        if (check_field[b, 0== check_field[b, 1&& check_field[b, 1== check_field[b, 2])
                        {
                            Console.WriteLine("승리!");
                            return;
                        }
                    }
 
 
                    Console.WriteLine("1, 2, 3");
                    Console.WriteLine("4, 5, 6");
                    Console.WriteLine("7, 8, 9");
                    Console.Write("위치 입력해주세요 :");
                    string tmp = Console.ReadLine();
 
                    //현재 누구 차례인지 따라 필드배열에 체크                                  
                    if (int.TryParse(tmp, out int a) && a > 0 && a < 10)
                    {
                        switch (int.Parse(tmp))
                        {
                            case 1:
                                if (fields[11!= 'ㅤ'continue;
                                fields[11= stemp;
                                check_field[00= stemp;
                                break;
                            case 2:
                                if (fields[13!= 'ㅤ'continue;
                                fields[13= stemp;
                                check_field[01= stemp;
                                break;
                            case 3:
                                if (fields[15!= 'ㅤ'continue;
                                fields[15= stemp;
                                check_field[02= stemp;
                                break;
                            case 4:
                                if (fields[31!= 'ㅤ'continue;
                                fields[31= stemp;
                                check_field[10= stemp;
                                break;
                            case 5:
                                if (fields[33!= 'ㅤ'continue;
                                fields[33= stemp;
                                check_field[11= stemp;
                                break;
                            case 6:
                                if (fields[35!= 'ㅤ'continue;
                                fields[35= stemp;
                                check_field[12= stemp;
                                break;
                            case 7:
                                if (fields[51!= 'ㅤ'continue;
                                fields[51= stemp;
                                check_field[20= stemp;
                                break;
                            case 8:
                                if (fields[53!= 'ㅤ'continue;
                                fields[53= stemp;
                                check_field[21= stemp;
                                break;
                            case 9:
                                if (fields[55!= 'ㅤ'continue;
                                fields[55= stemp;
                                check_field[22= stemp;
                                break;
 
                        }
                    }
                    else
                        continue;
 
 
                    //바톤터치
                    whosTurn = !whosTurn;
 
                    if (whosTurn == true)
                        stemp = '◆';
                    else
                        stemp = '◎';
 
                    count++;
                }
            }
        }
    }
}
 
cs
     


마우스 입력으로 받고 싶었으나 아직 안 익숙해서 포기

댓글 없음:

댓글 쓰기

c++ thread.h

 c++에서 쓰레드 돌릴려면 thread.h 헤더를 쓰면 되는데 이 친구는 쓰레드가 아직 실행 중인지, 아니면 강제 종료하거나 하는 함수가 없어서 조금 아쉬운 애다. std::thread 는 로컬 변수로 선언하든 new 동적 할당을 하든 start 함...