-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
56 lines (55 loc) · 1.79 KB
/
Program.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
using System;
namespace DesignTicTacToe
{
class Program
{
static void Main(string[] args)
{
var obj = new TicTacToe(3);
Console.WriteLine(obj.Move(0, 0, 1));
Console.WriteLine(obj.Move(0, 2, 2));
Console.WriteLine(obj.Move(2, 2, 1));
Console.WriteLine(obj.Move(1, 1, 2));
Console.WriteLine(obj.Move(2, 0, 1));
Console.WriteLine(obj.Move(1, 0, 2));
Console.WriteLine(obj.Move(2, 1, 1));
}
}
public class TicTacToe
{
private readonly int[][] board;
public TicTacToe(int n)
{
board = new int[n][];
for (int i = 0; i < board.Length; i++)
board[i] = new int[n];
}
public int Move(int row, int col, int player)
{
board[row][col] = player;
int[] dr = { -1, 1, 0, 0, -1, 1, -1, 1 };
int[] dc = { 0, 0, -1, 1, -1, 1, 1, -1 };
var record = new int[8];
for (int i = 0; i < 8; i++)
{
int count = 0;
for (int j = 1; j < board.Length; j++)
{
int newRow = row + dr[i] * j;
int newCol = col + dc[i] * j;
if (newRow < 0 || newRow >= board.Length || newCol < 0 || newCol >= board.Length || board[newRow][newCol] != player)
break;
if (board[newRow][newCol] == player)
count++;
}
record[i] = count;
}
for (int i = 0; i < record.Length - 1; i += 2)
{
if (record[i] + record[i + 1] == board.Length - 1)
return player;
}
return 0;
}
}
}