Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Program
- {
- static public void Main(string[] args)
- {
- Console.WriteLine("Welcome to tic-tac-toe!");
- Console.WriteLine("Press enter to begin...");
- Console.ReadLine();
- int[] Data = Init();
- int Turn = 1;
- while(!(bool)Victory(Data)[0])
- {
- switch (Turn)
- {
- case 1:
- Turn = 2;
- break;
- case 2:
- Turn = 1;
- break;
- }
- Draw(Data);
- Input(Turn, Data);
- }
- Draw(Data);
- Console.WriteLine(Symbol((int)Victory(Data)[1]) + " wins the game!");
- Console.ReadLine();
- }
- static private int[] Init()
- {
- return new int[9];
- }
- static private void Draw(int[] Data)
- {
- Console.Clear();
- Console.WriteLine("Controls:\n"); //Show controls
- Console.WriteLine("+-+-+-+");
- Console.WriteLine("|1|2|3|");
- Console.WriteLine("+-+-+-+");
- Console.WriteLine("|4|5|6|");
- Console.WriteLine("+-+-+-+");
- Console.WriteLine("|7|8|9|");
- Console.WriteLine("+-+-+-+\n\n");
- Console.WriteLine("Playing...\n"); //Show active board
- Console.WriteLine("+-+-+-+");
- Console.WriteLine("|" + Symbol(Data[0]) + "|" + Symbol(Data[1]) + "|" + Symbol(Data[2]) + "|");
- Console.WriteLine("+-+-+-+");
- Console.WriteLine("|" + Symbol(Data[3]) + "|" + Symbol(Data[4]) + "|" + Symbol(Data[5]) + "|");
- Console.WriteLine("+-+-+-+");
- Console.WriteLine("|" + Symbol(Data[6]) + "|" + Symbol(Data[7]) + "|" + Symbol(Data[8]) + "|");
- Console.WriteLine("+-+-+-+\n");
- }
- static private string Symbol(int Val)
- {
- switch (Val)
- {
- case 1:
- return "O";
- case 2:
- return "X";
- default:
- return " ";
- }
- }
- static private object[] Victory(int[] Data)
- {
- object[] Win = new object[2];
- Win[0] = false;
- Win[1] = 0;
- for (int i = 0; i < 9; i+=3) //Check for horizontal line
- {
- if (Data[i] == 0 || Data[i + 1] == 0 || Data[i + 2] == 0)
- continue;
- if (Data[i] == Data[i + 1] && Data[i + 1] == Data[i + 2])
- {
- Win[0] = true;
- Win[1] = Data[i];
- return Win;
- }
- }
- for (int i = 0; i < 3; i++) //Check for vertical line
- {
- if (Data[i] == 0 || Data[i + 3] == 0 || Data[i + 6] == 0)
- continue;
- if (Data[i] == Data[i + 3] && Data[i + 3] == Data[i + 6])
- {
- Win[0] = true;
- Win[1] = Data[i];
- return Win;
- }
- }
- if (Data[4] == 0)
- return Win;
- if (Data[0] == Data[4] && Data[4] == Data[8])
- {
- Win[0] = true;
- Win[1] = Data[4];
- return Win;
- }
- if (Data[6] == Data[4] && Data[4] == Data[2])
- {
- Win[0] = true;
- Win[1] = Data[4];
- return Win;
- }
- return Win;
- }
- static private void Input(int Turn, int[] Data)
- {
- Console.WriteLine("Place an " + Symbol(Turn));
- int Pos = 0;
- while (Pos == 0)
- {
- Console.WriteLine("Use numbers 1-9 to place");
- try
- {
- Pos = Convert.ToInt32(Console.ReadLine());
- }
- catch (FormatException)
- {
- Console.WriteLine("ERROR: Not an int");
- }
- catch (OverflowException)
- {
- Console.WriteLine("ERROR: Number too large");
- }
- if (1 <= Pos && Pos <= 9)
- {
- if (Data[Pos - 1] == 0)
- Data[Pos - 1] = Turn;
- else
- Pos = 0;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement