Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class HelloWorld {
- static void Main() {
- string [,] lines = {
- {"1", "2", "3"},
- {"4", "5", "6"},
- {"7", "8", "9"}
- };
- bool won = false, xWinner = false, oWinner = false, turn = false;
- string action;
- do {
- //Prints out the Board
- int f = 0;
- int t = 0;
- Console.Clear();
- foreach (string x in lines) {
- f++;
- if (f == lines.GetLength(1)) {
- t++;
- Console.WriteLine(x);
- if (t != lines.GetLength(0)) {
- for (int y = 0; y < 3 * lines.GetLength(1); y++) {
- Console.Write("-");
- }
- Console.Write("\n");
- }
- f = 0;
- } else {
- Console.Write(x + " | ");
- }
- };
- //Input
- action = Console.ReadLine();
- if (!(action == "X" || action == "x" || action == "O" || action == "o")) {
- int pop = 0;
- int bop = 0;
- foreach (string xx in lines) {
- pop++;
- if (pop > lines.GetLength(1)) {
- pop = 0;
- bop++;
- }
- if (lines[bop, pop] == action) {
- if (turn) {
- lines[bop, pop] = "X";
- } else {
- lines[bop, pop] = "O";
- }
- }
- }
- };
- //Checks for win
- for (int xxx = 0; xxx < lines.GetLength(0); xxx++) {
- for (int yyy = 0; yyy < lines.GetLength(1); yyy++) {
- //Checks horizontally
- if (lines.GetLength(1) - yyy < 3) {
- if ((lines[xxx, yyy] == lines[xxx, yyy + 1] && lines[xxx, yyy + 1] == lines[xxx, yyy + 2]) || (lines[xxx, yyy + 3] == lines[xxx - 1, yyy + 2] && lines[xxx - 1, yyy + 2] == lines[xxx, yyy + 3])) {
- if (lines[xxx, yyy] == "X") {
- xWinner = true;
- } else {
- oWinner = true;
- }
- won = true;
- }
- }
- //Checks Vertically
- if (lines.GetLength(0) - xxx < 3) {
- if (lines[xxx, yyy] == lines[xxx + 1, yyy] && lines[xxx + 1, yyy] == lines[xxx + 2, yyy]) {
- if (lines[xxx, yyy] == "X") {
- xWinner = true;
- } else {
- oWinner = true;
- }
- won = true;
- }
- }
- }
- }
- } while (!won);
- //Checks for no winner / draw / tie
- if (xWinner || oWinner) {
- if (xWinner) {
- Console.Write("X has won!");
- } else {
- Console.Write("O has won!");
- }
- } else {
- Console.Write("Nobody won!");
- }
- Console.ReadLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement