-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe
151 lines (104 loc) · 4.69 KB
/
TicTacToe
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
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static string[,] board ;
public static int n;
public static int w=0;
public static int player;
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("******TIC TAC TOE*****");
Console.WriteLine("Enter board dimension nxn ");
n = Int32.Parse( Console.ReadLine() );
board = new string[n,n];
int count_turns=1;
int col,row;
while (w==0 && count_turns<(n*n)){
player =count_turns%2!=0 ? 1: 2; // assign player 1 or 2
Console.WriteLine("Player "+ player);
Console.WriteLine(" Enter row # ");
row = Int32.Parse( Console.ReadLine() );
Console.WriteLine(" Enter column # ");
col = Int32.Parse( Console.ReadLine() );
w=PlacePiece(row,col,player);
count_turns++;
}
if (w==1){
Console.WriteLine("****PLAYER "+player+" Y O U W O N!!!*****");
}else{
Console.WriteLine("Nobody Won");
}
}
/// <summary>
/// Place a piece on the game
/// </summary>
/// <param name="row">row to place a piece</param> 0...n-1
/// <param name="col">column to place a piece</param>
/// <param name="player">the player (1 or 2) the piece is for</param>
/// <returns>0 = no winner, 1 = player 1 won, 2 = player 2 won</returns>
public static int PlacePiece(int row, int col, int player)
{
int winner=0;
int dimension=n;
board[row,col]= player==1 ? "X" : "O";
//Rows check
for(int i=0;i<dimension-1; i++){
if (board[row,i]==board[row,i+1] && board[row,i] !=null && board[row,i+1] !=null ){
winner++;
// Console.WriteLine("Rows check winner board [row "+row+", i ]"+i+"="+board[row,i]+" equal to "+board[row,i+1]);
}
}
// Console.WriteLine("Rows check winner "+winner+" dimension "+dimension);
if(winner==dimension-1){
return 1;
}else{
//Column check
winner=0;
for(int i=0;i<dimension-1; i++){
if (board[i,col]==board[i+1,col] && board[i,col]!=null && board[i+1,col]!=null ){
winner++;
}
}
//Console.WriteLine("Column check winner "+winner+" dimension "+dimension);
if(winner==dimension-1){
return 1;
}else{
winner=0;
//diagonal
for(int i=0;i<dimension-1; i++){
if (board[i,i]==board[i+1,i+1] && board[i,i]!=null && board[i+1,i+1]!=null){
winner++;
}
}
//Console.WriteLine("Diagonal check winner "+winner+" dimension "+dimension);
if(winner==dimension-1){
return 1;
}else{
winner=0;
//back diagonal
for(int i=0;i<dimension-1; i++){
if (board[i,dimension-i-1]==board[i+1,dimension-i-2] && board[i,dimension-i-1]!=null && board[i+1,dimension-i-2]!=null ){
winner++;
}
}
//Console.WriteLine("Back Diagonal check winner "+winner+" dimension "+dimension);
}
}
}
if(winner==dimension-1){
return 1;
}else{
return 0;
}
// return 0;
}
}
}