using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace cchamber { /// /// A form that can utilize the TicTacToe webservice to play Tic Tac Toe /// public partial class playForm : System.Web.UI.Page { protected com.harbormist.TicTacToe myWebService = null; // protected com.harbormist.TicTacToe const int NUM_CELLS = 9; const char EMPTY = '_'; // protected System.Web.UI.WebControls.Button Button2; //protected System.Web.UI.WebControls.Button Button3; //protected System.Web.UI.WebControls.Button Button4; //protected System.Web.UI.WebControls.Button Button5; //protected System.Web.UI.WebControls.Button Button6; //protected System.Web.UI.WebControls.Button Button7; //protected System.Web.UI.WebControls.Button Button1; //protected System.Web.UI.WebControls.Button Button0; //protected System.Web.UI.WebControls.Label titleLabel; //protected System.Web.UI.WebControls.Label statusLabel; //protected System.Web.UI.HtmlControls.HtmlInputHidden hiddenBoard; //protected System.Web.UI.WebControls.HyperLink playAgainLink; //protected System.Web.UI.WebControls.Label subtitleLabel; //protected System.Web.UI.WebControls.Button Button8; private void Page_Load(object sender, System.EventArgs e) { myWebService = new com.harbormist.TicTacToe(); //check for Board State String arguments given by the GET method String[] bSSs = Request.QueryString.GetValues("board"); if (bSSs == null || "".Equals(bSSs[0].Trim())) { //no prior board state, start a new game statusLabel.Text = "Welcome! You are 'X'"; return; } String bSS = bSSs[0]; //only need the first one of all the properly-named GET arguments this.hiddenBoard.Value = bSS; //keep the state to be sent next time showState(this.hiddenBoard.Value); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button8.Click += new System.EventHandler(this.Button8_Click); this.Button7.Click += new System.EventHandler(this.Button7_Click); this.Button6.Click += new System.EventHandler(this.Button6_Click); this.Button5.Click += new System.EventHandler(this.Button5_Click); this.Button4.Click += new System.EventHandler(this.Button4_Click); this.Button3.Click += new System.EventHandler(this.Button3_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Button0.Click += new System.EventHandler(this.Button0_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void showState(string boardState) { if (boardState.Length != NUM_CELLS) { //if we have an error here, soes the user really need to know? Perhaps not. //this.statusLabel.Text = "'" + boardState + "' does not have " + NUM_CELLS + " characters."; return; } Button0.Text = cellString(boardState[0]); Button1.Text = cellString(boardState[1]); Button2.Text = cellString(boardState[2]); Button3.Text = cellString(boardState[3]); Button4.Text = cellString(boardState[4]); Button5.Text = cellString(boardState[5]); Button6.Text = cellString(boardState[6]); Button7.Text = cellString(boardState[7]); Button8.Text = cellString(boardState[8]); this.hiddenBoard.Value = boardState; } /// /// Makes a char that represents the contents of a cell /// into a string that can be used as the text of a button. /// /// char representing the contents of a cell /// one-character string, to be used on a button private string cellString(char cellChar) { return cellChar == EMPTY ? " " : Convert.ToString(cellChar); } /// /// This is called by any button, with the index of the button as the argument. /// It calls the webservice to implement the user move, get the state after the /// computer move, and update the status line. /// /// The index of the button that was clicked (0-8) private void button_Click(int buttonNum) { String newState = myWebService.respondToUserMove(this.hiddenBoard.Value, buttonNum); this.statusLabel.Text = myWebService.getStatus(newState); showState(newState); } #region User Button Clicks // these routines call button_click(int) above. private void Button0_Click(object sender, System.EventArgs e) { button_Click(0); } private void Button1_Click(object sender, System.EventArgs e) { button_Click(1); } private void Button2_Click(object sender, System.EventArgs e) { button_Click(2); } private void Button3_Click(object sender, System.EventArgs e) { button_Click(3); } private void Button4_Click(object sender, System.EventArgs e) { button_Click(4); } private void Button5_Click(object sender, System.EventArgs e) { button_Click(5); } private void Button6_Click(object sender, System.EventArgs e) { button_Click(6); } private void Button7_Click(object sender, System.EventArgs e) { button_Click(7); } private void Button8_Click(object sender, System.EventArgs e) { button_Click(8); } #endregion } }