Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace _05TicTaeToe
- {
- public partial class TicTaeToe : System.Web.UI.Page
- {
- protected int[,] board; //3x3 is the size of Tic-Tac-Toe's board
- protected int turn;
- protected Dictionary<int[,], int> tictactoe = new Dictionary<int[,], int>();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (ViewState.Count > 0)//if (IsPostBack)
- {
- this.tictactoe = (Dictionary<int[,], int>)ViewState["tictactoe"];
- this.board = this.tictactoe.Keys.First();
- this.turn = this.tictactoe.Values.First();
- if ((this.turn % 2) == 1)
- {
- ComputerPlay();
- }
- }
- else
- {
- InitializeBoard();
- this.turn = 0;
- }
- }
- private void ComputerPlay()
- {
- Random rnd = new Random();
- int index = rnd.Next(0, 9);
- int[] position = GetRowAndCol(index);
- int row = position[0];
- int col = position[1];
- bool isAvailable = CheckIsAvailable(row, col);
- if (isAvailable)
- {
- FillComputerCell(index);
- this.board[row, col] = 2;
- this.turn++; //make it (turn % 2) == 0 to be a Human's turn
- }
- else
- {
- ComputerPlay();
- }
- }
- private void FillComputerCell(int index)
- {
- string path = "images/2.png";
- switch (index)
- {
- case 0: Imagebutton0.ImageUrl = path;
- break;
- case 1: Imagebutton1.ImageUrl = path;
- break;
- case 2: Imagebutton2.ImageUrl = path;
- break;
- case 3: Imagebutton3.ImageUrl = path;
- break;
- case 4: Imagebutton4.ImageUrl = path;
- break;
- case 5: Imagebutton5.ImageUrl = path;
- break;
- case 6: Imagebutton6.ImageUrl = path;
- break;
- case 7: Imagebutton7.ImageUrl = path;
- break;
- case 8: Imagebutton8.ImageUrl = path;
- break;
- default: throw new BadImageFormatException("Bad image ID!");
- }
- }
- private void InitializeBoard()
- {
- int filler = int.MinValue;
- this.board = new int[3, 3];
- for (int i = 0; i < this.board.GetLength(0); i++)
- {
- for (int j = 0; j < this.board.GetLength(1); j++)
- {
- this.board[i, j] = filler;
- filler++;
- }
- }
- }
- protected void Page_PreRender(object sender, EventArgs e)
- {
- if (this.tictactoe.ContainsKey(this.board))
- {
- this.tictactoe[this.board] = this.turn;
- }
- else
- {
- this.tictactoe.Add(this.board, this.turn);
- }
- ViewState["tictactoe"] = this.tictactoe;
- }
- #region ImageButtons_Clicked
- protected void Imagebutton0_click(object sender, EventArgs e)
- {
- Play(0, this.Imagebutton0);
- }
- protected void Imagebutton1_click(object sender, EventArgs e)
- {
- Play(1, this.Imagebutton1);
- }
- protected void Imagebutton2_click(object sender, EventArgs e)
- {
- Play(2, this.Imagebutton2);
- }
- protected void Imagebutton3_click(object sender, EventArgs e)
- {
- Play(3, this.Imagebutton3);
- }
- protected void Imagebutton4_click(object sender, EventArgs e)
- {
- Play(4, this.Imagebutton4);
- }
- protected void Imagebutton5_click(object sender, EventArgs e)
- {
- Play(5, this.Imagebutton5);
- }
- protected void Imagebutton6_click(object sender, EventArgs e)
- {
- Play(6, this.Imagebutton6);
- }
- protected void Imagebutton7_click(object sender, EventArgs e)
- {
- Play(7, this.Imagebutton7);
- }
- protected void Imagebutton8_click(object sender, EventArgs e)
- {
- Play(8, this.Imagebutton8);
- }
- #endregion
- protected void Play(int index, ImageButton clickedImage)
- {
- bool isFinished = false;
- if (this.turn != 0)
- {
- isFinished = CheckForFinish();
- }
- if (!isFinished)
- {
- int[] position = GetRowAndCol(index);
- int row = position[0];
- int col = position[1];
- bool isAvailable = CheckIsAvailable(row, col);
- if (isAvailable)
- {
- this.board[row, col] = 1;
- FillCell(clickedImage);
- this.turn++; //make it (turn % 2) == 1 to be a PC's turn
- }
- else
- {
- Play(index, clickedImage);
- }
- }
- else
- {
- Response.Write("The game is finished!"); //just an example
- }
- }
- private void FillCell(ImageButton clickedImage)
- {
- clickedImage.ImageUrl = "images/1.png";
- }
- #region Checks For Finishing
- private bool CheckForFinish()
- {
- bool isFinishedByRow = CheckRow();
- bool isFinishedByCol = CheckCol();
- bool isFinishedByDiagonal = CheckDiagonal();
- int counter = 0;
- if (isFinishedByRow)
- {
- counter++;
- }
- if (isFinishedByCol)
- {
- counter++;
- }
- if (isFinishedByDiagonal)
- {
- counter++;
- }
- if (counter == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- private bool CheckDiagonal()
- {
- if (this.board[0, 0] == this.board[1, 1] && this.board[1, 1] == this.board[2, 2])
- {
- return true;
- }
- if (this.board[0, 2] == this.board[1, 1] && this.board[2, 0] == this.board[1, 1])
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private bool CheckCol()
- {
- for (int i = 0; i < this.board.GetLength(0); i++)
- {
- bool isFinished = true;
- for (int j = 0; j < this.board.GetLength(1) - 1; j++)
- {
- if (this.board[j, i] == this.board[j + 1, i])
- {
- continue;
- }
- else
- {
- isFinished = false;
- break;
- }
- }
- if (isFinished)
- {
- return true;
- }
- }
- return false;
- }
- private bool CheckRow()
- {
- for (int i = 0; i < this.board.GetLength(0); i++)
- {
- bool isFinished = true;
- for (int j = 0; j < this.board.GetLength(1) - 1; j++)
- {
- if (this.board[i, j] == this.board[i, j + 1])
- {
- continue;
- }
- else
- {
- isFinished = false;
- break;
- }
- }
- if (isFinished)
- {
- return true;
- }
- }
- return false;
- }
- #endregion
- private bool CheckIsAvailable(int row, int col)
- {
- if (this.board[row, col] != 1 && this.board[row, col] != 2)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- private int[] GetRowAndCol(int index)
- {
- int row;
- if (index < 3)
- {
- row = 0;
- }
- if (index >= 3 && index <= 5)
- {
- row = 1;
- }
- else
- {
- row = 2;
- }
- int col = index % 3;
- int[] position = new int[2];
- position[0] = row;
- position[1] = col;
- return position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment