Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static public void Main(string[] args)
  11.         {
  12.             Console.WriteLine("Welcome to tic-tac-toe!");
  13.             Console.WriteLine("Press enter to begin...");
  14.             Console.ReadLine();
  15.             int[] Data = Init();
  16.             int Turn = 1;
  17.             while(!(bool)Victory(Data)[0])
  18.             {
  19.                 switch (Turn)
  20.                 {
  21.                     case 1:
  22.                         Turn = 2;
  23.                         break;
  24.                     case 2:
  25.                         Turn = 1;
  26.                         break;
  27.                 }
  28.                 Draw(Data);
  29.                 Input(Turn, Data);
  30.             }
  31.             Draw(Data);
  32.             Console.WriteLine(Symbol((int)Victory(Data)[1]) + " wins the game!");
  33.             Console.ReadLine();
  34.         }
  35.         static private int[] Init()
  36.         {
  37.             return new int[9];
  38.         }
  39.         static private void Draw(int[] Data)
  40.         {
  41.             Console.Clear();
  42.             Console.WriteLine("Controls:\n"); //Show controls
  43.             Console.WriteLine("+-+-+-+");
  44.             Console.WriteLine("|1|2|3|");
  45.             Console.WriteLine("+-+-+-+");
  46.             Console.WriteLine("|4|5|6|");
  47.             Console.WriteLine("+-+-+-+");
  48.             Console.WriteLine("|7|8|9|");
  49.             Console.WriteLine("+-+-+-+\n\n");
  50.  
  51.             Console.WriteLine("Playing...\n"); //Show active board
  52.             Console.WriteLine("+-+-+-+");
  53.             Console.WriteLine("|" + Symbol(Data[0]) + "|" + Symbol(Data[1]) + "|" + Symbol(Data[2]) + "|");
  54.             Console.WriteLine("+-+-+-+");
  55.             Console.WriteLine("|" + Symbol(Data[3]) + "|" + Symbol(Data[4]) + "|" + Symbol(Data[5]) + "|");
  56.             Console.WriteLine("+-+-+-+");
  57.             Console.WriteLine("|" + Symbol(Data[6]) + "|" + Symbol(Data[7]) + "|" + Symbol(Data[8]) + "|");
  58.             Console.WriteLine("+-+-+-+\n");
  59.         }
  60.         static private string Symbol(int Val)
  61.         {
  62.             switch (Val)
  63.             {
  64.                 case 1:
  65.                         return "O";
  66.                 case 2:
  67.                         return "X";
  68.                 default:
  69.                         return " ";
  70.             }
  71.         }
  72.         static private object[] Victory(int[] Data)
  73.         {
  74.             object[] Win = new object[2];
  75.             Win[0] = false;
  76.             Win[1] = 0;
  77.             for (int i = 0; i < 9; i+=3) //Check for horizontal line
  78.             {
  79.                 if (Data[i] == 0 || Data[i + 1] == 0 || Data[i + 2] == 0)
  80.                     continue;
  81.                 if (Data[i] == Data[i + 1] && Data[i + 1] == Data[i + 2])
  82.                 {
  83.                     Win[0] = true;
  84.                     Win[1] = Data[i];
  85.                     return Win;
  86.                 }
  87.             }
  88.             for (int i = 0; i < 3; i++) //Check for vertical line
  89.             {
  90.                 if (Data[i] == 0 || Data[i + 3] == 0 || Data[i + 6] == 0)
  91.                     continue;
  92.                 if (Data[i] == Data[i + 3] && Data[i + 3] == Data[i + 6])
  93.                 {
  94.                     Win[0] = true;
  95.                     Win[1] = Data[i];
  96.                     return Win;
  97.                 }
  98.             }
  99.             if (Data[4] == 0)
  100.                 return Win;
  101.             if (Data[0] == Data[4] && Data[4] == Data[8])
  102.             {
  103.                 Win[0] = true;
  104.                 Win[1] = Data[4];
  105.                 return Win;
  106.             }
  107.             if (Data[6] == Data[4] && Data[4] == Data[2])
  108.             {
  109.                 Win[0] = true;
  110.                 Win[1] = Data[4];
  111.                 return Win;
  112.             }
  113.             return Win;
  114.         }
  115.         static private void Input(int Turn, int[] Data)
  116.         {
  117.             Console.WriteLine("Place an " + Symbol(Turn));
  118.             int Pos = 0;
  119.             while (Pos == 0)
  120.             {
  121.                 Console.WriteLine("Use numbers 1-9 to place");
  122.                 try
  123.                 {
  124.                     Pos = Convert.ToInt32(Console.ReadLine());
  125.                 }
  126.                 catch (FormatException)
  127.                 {
  128.                     Console.WriteLine("ERROR: Not an int");
  129.                 }
  130.                 catch (OverflowException)
  131.                 {
  132.                     Console.WriteLine("ERROR: Number too large");
  133.                 }
  134.                 if (1 <= Pos && Pos <= 9)
  135.                 {
  136.                     if (Data[Pos - 1] == 0)
  137.                         Data[Pos - 1] = Turn;
  138.                     else
  139.                         Pos = 0;
  140.                 }
  141.             }
  142.         }
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement