TodorMitev

TicTaeToe.aspx.cs

May 18th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. namespace _05TicTaeToe
  9. {
  10.     public partial class TicTaeToe : System.Web.UI.Page
  11.     {
  12.         protected int[,] board; //3x3 is the size of Tic-Tac-Toe's board
  13.         protected int turn;
  14.  
  15.         protected Dictionary<int[,], int> tictactoe = new Dictionary<int[,], int>();
  16.  
  17.         protected void Page_Load(object sender, EventArgs e)
  18.         {
  19.             if (ViewState.Count > 0)//if (IsPostBack)
  20.             {
  21.                 this.tictactoe = (Dictionary<int[,], int>)ViewState["tictactoe"];
  22.                 this.board = this.tictactoe.Keys.First();
  23.                 this.turn = this.tictactoe.Values.First();
  24.  
  25.                 if ((this.turn % 2) == 1)
  26.                 {
  27.                     ComputerPlay();
  28.                 }
  29.             }
  30.             else
  31.             {
  32.                 InitializeBoard();
  33.                 this.turn = 0;
  34.             }
  35.         }
  36.  
  37.         private void ComputerPlay()
  38.         {
  39.             Random rnd = new Random();
  40.             int index = rnd.Next(0, 9);
  41.             int[] position = GetRowAndCol(index);
  42.             int row = position[0];
  43.             int col = position[1];
  44.  
  45.             bool isAvailable = CheckIsAvailable(row, col);
  46.             if (isAvailable)
  47.             {
  48.                 FillComputerCell(index);
  49.                 this.board[row, col] = 2;
  50.                 this.turn++; //make it (turn % 2) == 0 to be a Human's turn
  51.             }
  52.             else
  53.             {
  54.                 ComputerPlay();
  55.             }
  56.         }
  57.  
  58.         private void FillComputerCell(int index)
  59.         {
  60.             string path = "images/2.png";
  61.             switch (index)
  62.             {
  63.                 case 0: Imagebutton0.ImageUrl = path;
  64.                     break;
  65.                 case 1: Imagebutton1.ImageUrl = path;
  66.                     break;
  67.                 case 2: Imagebutton2.ImageUrl = path;
  68.                     break;
  69.                 case 3: Imagebutton3.ImageUrl = path;
  70.                     break;
  71.                 case 4: Imagebutton4.ImageUrl = path;
  72.                     break;
  73.                 case 5: Imagebutton5.ImageUrl = path;
  74.                     break;
  75.                 case 6: Imagebutton6.ImageUrl = path;
  76.                     break;
  77.                 case 7: Imagebutton7.ImageUrl = path;
  78.                     break;
  79.                 case 8: Imagebutton8.ImageUrl = path;
  80.                     break;
  81.                 default: throw new BadImageFormatException("Bad image ID!");
  82.             }
  83.         }
  84.  
  85.         private void InitializeBoard()
  86.         {
  87.             int filler = int.MinValue;
  88.             this.board = new int[3, 3];
  89.             for (int i = 0; i < this.board.GetLength(0); i++)
  90.             {
  91.                 for (int j = 0; j < this.board.GetLength(1); j++)
  92.                 {
  93.                     this.board[i, j] = filler;
  94.                     filler++;
  95.                 }
  96.             }
  97.         }
  98.  
  99.         protected void Page_PreRender(object sender, EventArgs e)
  100.         {
  101.             if (this.tictactoe.ContainsKey(this.board))
  102.             {
  103.                 this.tictactoe[this.board] = this.turn;
  104.             }
  105.             else
  106.             {
  107.                 this.tictactoe.Add(this.board, this.turn);
  108.             }
  109.             ViewState["tictactoe"] = this.tictactoe;
  110.         }
  111.         #region ImageButtons_Clicked
  112.  
  113.         protected void Imagebutton0_click(object sender, EventArgs e)
  114.         {
  115.             Play(0, this.Imagebutton0);
  116.         }
  117.  
  118.         protected void Imagebutton1_click(object sender, EventArgs e)
  119.         {
  120.             Play(1, this.Imagebutton1);
  121.         }
  122.  
  123.         protected void Imagebutton2_click(object sender, EventArgs e)
  124.         {
  125.             Play(2, this.Imagebutton2);
  126.         }
  127.  
  128.         protected void Imagebutton3_click(object sender, EventArgs e)
  129.         {
  130.             Play(3, this.Imagebutton3);
  131.         }
  132.  
  133.         protected void Imagebutton4_click(object sender, EventArgs e)
  134.         {
  135.             Play(4, this.Imagebutton4);
  136.         }
  137.  
  138.         protected void Imagebutton5_click(object sender, EventArgs e)
  139.         {
  140.             Play(5, this.Imagebutton5);
  141.         }
  142.  
  143.         protected void Imagebutton6_click(object sender, EventArgs e)
  144.         {
  145.             Play(6, this.Imagebutton6);
  146.         }
  147.  
  148.         protected void Imagebutton7_click(object sender, EventArgs e)
  149.         {
  150.             Play(7, this.Imagebutton7);
  151.         }
  152.  
  153.         protected void Imagebutton8_click(object sender, EventArgs e)
  154.         {
  155.             Play(8, this.Imagebutton8);
  156.         }
  157.  
  158.         #endregion
  159.  
  160.         protected void Play(int index, ImageButton clickedImage)
  161.         {
  162.             bool isFinished = false;
  163.             if (this.turn != 0)
  164.             {
  165.                 isFinished = CheckForFinish();
  166.             }
  167.  
  168.             if (!isFinished)
  169.             {
  170.                 int[] position = GetRowAndCol(index);
  171.                 int row = position[0];
  172.                 int col = position[1];
  173.  
  174.                 bool isAvailable = CheckIsAvailable(row, col);
  175.                 if (isAvailable)
  176.                 {
  177.                     this.board[row, col] = 1;
  178.                     FillCell(clickedImage);
  179.                     this.turn++; //make it (turn % 2) == 1 to be a PC's turn
  180.                 }
  181.                 else
  182.                 {
  183.                     Play(index, clickedImage);
  184.                 }
  185.             }
  186.             else
  187.             {
  188.                 Response.Write("The game is finished!"); //just an example
  189.             }
  190.         }
  191.  
  192.         private void FillCell(ImageButton clickedImage)
  193.         {
  194.             clickedImage.ImageUrl = "images/1.png";
  195.         }
  196.  
  197.         #region Checks For Finishing
  198.         private bool CheckForFinish()
  199.         {
  200.             bool isFinishedByRow = CheckRow();
  201.             bool isFinishedByCol = CheckCol();
  202.             bool isFinishedByDiagonal = CheckDiagonal();
  203.             int counter = 0;
  204.             if (isFinishedByRow)
  205.             {
  206.                 counter++;
  207.             }
  208.             if (isFinishedByCol)
  209.             {
  210.                 counter++;
  211.             }
  212.             if (isFinishedByDiagonal)
  213.             {
  214.                 counter++;
  215.             }
  216.  
  217.             if (counter == 0)
  218.             {
  219.                 return false;
  220.             }
  221.             else
  222.             {
  223.                 return true;
  224.             }
  225.  
  226.         }
  227.  
  228.         private bool CheckDiagonal()
  229.         {
  230.             if (this.board[0, 0] == this.board[1, 1] && this.board[1, 1] == this.board[2, 2])
  231.             {
  232.                 return true;
  233.             }
  234.             if (this.board[0, 2] == this.board[1, 1] && this.board[2, 0] == this.board[1, 1])
  235.             {
  236.                 return true;
  237.             }
  238.             else
  239.             {
  240.                 return false;
  241.             }
  242.         }
  243.  
  244.         private bool CheckCol()
  245.         {
  246.             for (int i = 0; i < this.board.GetLength(0); i++)
  247.             {
  248.                 bool isFinished = true;
  249.                 for (int j = 0; j < this.board.GetLength(1) - 1; j++)
  250.                 {
  251.                     if (this.board[j, i] == this.board[j + 1, i])
  252.                     {
  253.                         continue;
  254.                     }
  255.                     else
  256.                     {
  257.                         isFinished = false;
  258.                         break;
  259.                     }
  260.                 }
  261.                 if (isFinished)
  262.                 {
  263.                     return true;
  264.                 }
  265.             }
  266.             return false;
  267.         }
  268.  
  269.         private bool CheckRow()
  270.         {
  271.             for (int i = 0; i < this.board.GetLength(0); i++)
  272.             {
  273.                 bool isFinished = true;
  274.                 for (int j = 0; j < this.board.GetLength(1) - 1; j++)
  275.                 {
  276.                     if (this.board[i, j] == this.board[i, j + 1])
  277.                     {
  278.                         continue;
  279.                     }
  280.                     else
  281.                     {
  282.                         isFinished = false;
  283.                         break;
  284.                     }
  285.                 }
  286.                 if (isFinished)
  287.                 {
  288.                     return true;
  289.                 }
  290.             }
  291.             return false;
  292.         }
  293.         #endregion
  294.         private bool CheckIsAvailable(int row, int col)
  295.         {
  296.             if (this.board[row, col] != 1 && this.board[row, col] != 2)
  297.             {
  298.                 return true;
  299.             }
  300.             else
  301.             {
  302.                 return false;
  303.             }
  304.         }
  305.  
  306.         private int[] GetRowAndCol(int index)
  307.         {
  308.             int row;
  309.             if (index < 3)
  310.             {
  311.                 row = 0;
  312.             }
  313.             if (index >= 3 && index <= 5)
  314.             {
  315.                 row = 1;
  316.             }
  317.             else
  318.             {
  319.                 row = 2;
  320.             }
  321.             int col = index % 3;
  322.  
  323.             int[] position = new int[2];
  324.             position[0] = row;
  325.             position[1] = col;
  326.             return position;
  327.         }
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment