Hi,
OK, I've managed to work out how to set the coordinates for each position on the board. But I still cannot get the combo boxes to paint the selected token onto the board in the correct coordinate.
1. I'm supposed to use an enumerated data type to represent the states of the board positions eg enum BoxState{Blank, Naught, Cross}.
2. The interface has to be provided as an indexer with two parameters eg
board[0,0] = TicTacToe.Boxstate.Naught
3. I still cant get the grid lines to resize correctly.
this is where my code falls apart. I dont understand how to write code for 1 and 2 and 3.
Any analysis of my (rather poor) code and help here would be much appreciated.
Thanks very much.
APPLICATION.CS
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Assignment6
{
class TicTacToeApplication : Form
{
public static void Main(string[] args)
{
Application.Run(new TicTacToeApplication());
}
ComboBox comboBox1;
ComboBox comboBox2;
ComboBox comboBox3;
ComboBox comboBox4;
ComboBox comboBox5;
ComboBox comboBox6;
ComboBox comboBox7;
ComboBox comboBox8;
ComboBox comboBox9;
private TicTacToeGrid tttGrid;
public void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.comboBox3 = new System.Windows.Forms.ComboBox();
this.comboBox4 = new System.Windows.Forms.ComboBox();
this.comboBox5 = new System.Windows.Forms.ComboBox();
this.comboBox6 = new System.Windows.Forms.ComboBox();
this.comboBox7 = new System.Windows.Forms.ComboBox();
this.comboBox8 = new System.Windows.Forms.ComboBox();
this.comboBox9 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox1.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox1.Location = new System.Drawing.Point(328, 16);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(96, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.Text = "blank";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// comboBox2
//
this.comboBox2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox2.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox2.Location = new System.Drawing.Point(440, 16);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(80, 21);
this.comboBox2.TabIndex = 1;
this.comboBox2.Text = "blank";
//this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// comboBox3
//
this.comboBox3.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox3.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox3.Location = new System.Drawing.Point(536, 16);
this.comboBox3.Name = "comboBox3";
this.comboBox3.Size = new System.Drawing.Size(80, 21);
this.comboBox3.TabIndex = 2;
this.comboBox3.Text = "blank";
//this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// comboBox4
//
this.comboBox4.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox4.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox4.Location = new System.Drawing.Point(328, 48);
this.comboBox4.Name = "comboBox4";
this.comboBox4.Size = new System.Drawing.Size(96, 21);
this.comboBox4.TabIndex = 3;
this.comboBox4.Text = "blank";
//this.comboBox4.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// comboBox5
//
this.comboBox5.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox5.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox5.Location = new System.Drawing.Point(440, 48);
this.comboBox5.Name = "comboBox5";
this.comboBox5.Size = new System.Drawing.Size(80, 21);
this.comboBox5.TabIndex = 4;
this.comboBox5.Text = "blank";
//this.comboBox5.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// comboBox6
//
this.comboBox6.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox6.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox6.Location = new System.Drawing.Point(536, 48);
this.comboBox6.Name = "comboBox6";
this.comboBox6.Size = new System.Drawing.Size(80, 21);
this.comboBox6.TabIndex = 5;
this.comboBox6.Text = "blank";
//this.comboBox6.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// comboBox7
//
this.comboBox7.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox7.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox7.Location = new System.Drawing.Point(328, 80);
this.comboBox7.Name = "comboBox7";
this.comboBox7.Size = new System.Drawing.Size(96, 21);
this.comboBox7.TabIndex = 6;
this.comboBox7.Text = "blank";
//this.comboBox7.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// comboBox8
//
this.comboBox8.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox8.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox8.Location = new System.Drawing.Point(440, 80);
this.comboBox8.Name = "comboBox8";
this.comboBox8.Size = new System.Drawing.Size(80, 21);
this.comboBox8.TabIndex = 7;
this.comboBox8.Text = "blank";
//this.comboBox8.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);
//
// comboBox9
//
this.comboBox9.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
this.comboBox9.Items.AddRange(new object[] {
"blank",
"X",
"O"});
this.comboBox9.Location = new System.Drawing.Point(536, 80);
this.comboBox9.Name = "comboBox9";
this.comboBox9.Size = new System.Drawing.Size(80, 21);
this.comboBox9.TabIndex = 8;
this.comboBox9.Text = "blank";
//
// TicTacToeApplication
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(656, 342);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox9,
this.comboBox8,
this.comboBox7,
this.comboBox6,
this.comboBox5,
this.comboBox4,
this.comboBox3,
this.comboBox2,
this.comboBox1});
this.Name = "TicTacToeApplication";
this.ResumeLayout(false);
}
public TicTacToeApplication()
{
// Set the size of the form and a minimum size to ensure our Tic
// Tac Toe grid doesn't get too small.
this.Size = new Size(500, 500);
this.MinimumSize = new Size(300, 300);
// Create the Tic Tac Toe grid
tttGrid = new TicTacToeGrid();
// Set its location to just inside the top right margin
tttGrid.Location = new Point(8, 8);
// Set the anchor points so that the control resizes with the form
tttGrid.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
// Add the Tic Tac Toe grid to the form
this.Controls.Add(tttGrid);
InitializeComponent();
this.Name = "TicTacToeApplication";
this.ResumeLayout(false);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(sender.Equals(comboBox1.SelectedIndex = 0))
{
}
}
}
}
TICTACTOW.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Assignment6
{
public class TicTacToeGrid : Control
{
public enum BoxState{Blank, Naught, Cross };
int[,] board = new int[3,3];
private BoxState fSelection;
public BoxState Selection
{
get { return this.fSelection;}
}
/// <summary>
/// Constructor sets the ResizeRedraw style to force the control to be
/// redrawn whenever it is resized, i.e., the client area is invalidated
/// </summary>
public TicTacToeGrid()
{
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
/// <summary>
/// This property sets the default starting size for a control. If you don't
/// set this, the default is a control that is 0x0, i.e., not visible.
/// </summary>
protected override Size DefaultSize
{
get
{
return new Size(250, 250);
}
}
/// <summary>
/// This method is invoked automatically by Windows when the control needs to
/// be redrawn. NEVER invoke this method yourself. Instead, when the state
/// of the control changes you invoke the Invalidate() method to notify
/// Windows that this control needs to be redrawn.
/// </summary>
/// <param name="e">Provides the area on which you can draw your control, often
/// referred to as a canvas</param>
protected override void OnPaint(PaintEventArgs e)
{
Graphics canvas = e.Graphics;
Pen myPen = new Pen(this.ForeColor,3); // ForeColor is black by default, equivalent to Color.Black
// Draws a line from the top left corner to the bottom right corner
canvas.DrawLine(myPen, this.ClientRectangle.Width/3, 0, this.ClientRectangle.Width/3,this.ClientRectangle.Height - 1);
// Draws a line from the top right corner to the bottom left corner
canvas.DrawLine(myPen, this.ClientRectangle.Width/3 * 2, 0, this.ClientRectangle.Width/3 * 2, this.ClientRectangle.Height - 1);
// Draws a line from the top left corner to the bottom right corner
canvas.DrawLine(myPen, 0, this.ClientRectangle.Width/3,this.ClientRectangle.Height - 1,this.ClientRectangle.Width/3);
// Draws a line from the top right corner to the bottom left corner
canvas.DrawLine(myPen, 0, this.ClientRectangle.Width/3 * 2, this.ClientRectangle.Height - 1,this.ClientRectangle.Width/3 * 2);
//Draw a nought
//Rectangle rect = new Rectangle(70,70,70,70);
canvas.DrawEllipse(myPen,0,0,this.ClientRectangle.Width/3, this.ClientRectangle.Height/3 );
//Draw a cross
canvas.DrawLine(myPen,0,0,this.ClientRectangle.Width/3, this.ClientRectangle.Height/3);
canvas.DrawLine(myPen,this.ClientRectangle.Width/3,0,0, this.ClientRectangle.Height/3);
canvas.DrawEllipse(myPen,this.ClientRectangle.Width/3*2,0,this.ClientRectangle.Width/3, this.ClientRectangle.Height/3);
// Draws a rectangle around the outside edges of the control. Note
// that the width and height of the control's client area (visible area)
// have one subtracted as the x and y coordinates start at zero
canvas.DrawRectangle(myPen, 0, 0, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1);
base.OnPaint (e);
}
}
}

1 answers
This looks very similar to an assignment at Deakin university. You wouldn't happen to go there would you????? :)
Just be careful mate, Justin isn't silly.
answered 9 months ago by:
0
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Ass6
{
public class TicTacToeGrid : Control
{
/// <summary>
/// Constructor sets the ResizeRedraw style to force the control to be
/// redrawn whenever it is resized, i.e., the client area is invalidated
/// </summary>
///
private BoxState[,] board;
public TicTacToeGrid()
{
this.SetStyle(ControlStyles.ResizeRedraw, true);
board = new BoxState[3,3];
}
/// <summary>
/// This property sets the default starting size for a control. If you don't
/// set this, the default is a control that is 0x0, i.e., not visible.
/// </summary>
protected override Size DefaultSize
{
get
{
return new Size(250, 250);
}
}
public enum BoxState
{
Blank, Naught, Cross
}
/// <summary>
/// This method is invoked automatically by Windows when the control needs to
/// be redrawn. NEVER invoke this method yourself. Instead, when the state
/// of the control changes you invoke the Invalidate() method to notify
/// Windows that this control needs to be redrawn.
/// </summary>
/// <param name="e">Provides the area on which you can draw your control, often
/// referred to as a canvas</param>
protected override void OnPaint(PaintEventArgs e)
{
Graphics canvas = e.Graphics;
DrawBoard(canvas);
/*
// Draws a rectangle around the outside edges of the control. Note
// that the width and height of the control's client area (visible area)
// have one subtracted as the x and y coordinates start at zero
canvas.DrawRectangle(myPen, 0, 0, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1);
*/
base.OnPaint (e);
}
public void ChangeBoard(int Location,BoxState newBoxState)
{
if(Location < 3)
{
board[0,Location] = newBoxState;
}
else if(Location < 6)
{
board[1,Location-3] = newBoxState;
}
else
{
board[2,Location-6] = newBoxState;
}
DrawBoard(this.CreateGraphics());
}
public void DrawBoard(Graphics canvas)
{
Pen myPen = new Pen(this.ForeColor, 4); // ForeColor is black by default, equivalent to Color.Black
//Draw the fuckin board
// Vertical 1
canvas.DrawLine(myPen, this.ClientRectangle.Width / 3 * 1, 0, this.ClientRectangle.Width / 3 * 1, this.ClientRectangle.Height);
// Vertical 2
canvas.DrawLine(myPen, this.ClientRectangle.Width / 3 * 2, 0, this.ClientRectangle.Width / 3 * 2, this.ClientRectangle.Height);
// Horizontal 1
canvas.DrawLine(myPen, 0, this.ClientRectangle.Height / 3 * 1, this.ClientRectangle.Width, this.ClientRectangle.Height / 3 * 1);
// Horizontal 2
canvas.DrawLine(myPen, 0, this.ClientRectangle.Height / 3 * 2, this.ClientRectangle.Width, this.ClientRectangle.Height / 3 * 2);
//Draw the shapes on the board
for(int i=0;i<=2;i++)
{
for(int j=0;j<=2;j++)
{
switch(board[i,j])
{
case BoxState.Blank:
{
//clean out
//make circle in same window color
//make cross in same window color
break;
}
case BoxState.Cross:
{
//Draw a cross
canvas.DrawLine(myPen,0,0,this.ClientRectangle.Width/3, this.ClientRectangle.Height/3);
canvas.DrawLine(myPen,this.ClientRectangle.Width/3,0,0, this.ClientRectangle.Height/3);
canvas.DrawEllipse(myPen,this.ClientRectangle.Width/3*2,0,this.ClientRectangle.Width/3, this.ClientRectangle.Height/3);
break;
}
case BoxState.Naught:
{
//idea is
canvas.DrawEllipse(myPen,i,j,this.ClientRectangle.Width/3, this.ClientRectangle.Height/3 );
break;
}
}
}
}
}
}
}
and the other file:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Ass6
{
class TicTacToeApplication : Form
{
public static void Main(string[] args)
{
Application.Run(new TicTacToeApplication());
}
private TicTacToeGrid tttGrid;
public TicTacToeApplication()
{
this.SuspendLayout();
// Set the size of the form and a minimum size to ensure our Tic Tac Toe grid doesn't get too small.
this.Size = new Size(600, 350);
this.MinimumSize = new Size(600, 350);
this.Text = "SIT131 Assignment 6";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
// Create the Tic Tac Toe grid
tttGrid = new TicTacToeGrid();
// Set its location to just inside the top right margin
tttGrid.Location = new Point(16, 16);
// Set the anchor points so that the control resizes with the form
tttGrid.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
// Add the Tic Tac Toe grid to the form
this.Controls.Add(tttGrid);
// Combo Box 1
ComboBox myComboBox1 = new ComboBox();
myComboBox1.Location = new Point(330, 40);
myComboBox1.Size = new Size(60, 15);
myComboBox1.Text = "blank";
myComboBox1.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox1.TabIndex = 0;
myComboBox1.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox1.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox1);
// Combo Box 2
ComboBox myComboBox2 = new ComboBox();
myComboBox2.Location = new Point(400, 40);
myComboBox2.Size = new Size(60, 15);
myComboBox2.Text = "blank";
myComboBox2.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox2.TabIndex = 1;
myComboBox2.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox2.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox2);
// Combo Box 3
ComboBox myComboBox3 = new ComboBox();
myComboBox3.Location = new Point(470, 40);
myComboBox3.Size = new Size(60, 15);
myComboBox3.Text = "blank";
myComboBox3.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox3.TabIndex = 2;
myComboBox3.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox3.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox3);
// Combo Box 4
ComboBox myComboBox4 = new ComboBox();
myComboBox4.Location = new Point(330, 70);
myComboBox4.Size = new Size(60, 15);
myComboBox4.Text = "blank";
myComboBox4.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox4.TabIndex = 3;
myComboBox4.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox4.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox4);
// Combo Box 5
ComboBox myComboBox5 = new ComboBox();
myComboBox5.Location = new Point(400, 70);
myComboBox5.Size = new Size(60, 15);
myComboBox5.Text = "blank";
myComboBox5.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox5.TabIndex = 4;
myComboBox5.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox5.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox5);
// Combo Box 6
ComboBox myComboBox6 = new ComboBox();
myComboBox6.Location = new Point(470, 70);
myComboBox6.Size = new Size(60, 15);
myComboBox6.Text = "blank";
myComboBox6.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox6.TabIndex = 5;
myComboBox6.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox6.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox6);
// Combo Box 7
ComboBox myComboBox7 = new ComboBox();
myComboBox7.Location = new Point(330, 100);
myComboBox7.Size = new Size(60, 15);
myComboBox7.Text = "blank";
myComboBox7.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox7.TabIndex = 6;
myComboBox7.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox7.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox7);
// Combo Box 8
ComboBox myComboBox8 = new ComboBox();
myComboBox8.Location = new Point(400, 100);
myComboBox8.Size = new Size(60, 15);
myComboBox8.Text = "blank";
myComboBox8.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox8.TabIndex = 7;
myComboBox8.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox8.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox8);
// Combo Box 9
ComboBox myComboBox9 = new ComboBox();
myComboBox9.Location = new Point(470, 100);
myComboBox9.Size = new Size(60, 15);
myComboBox9.Text = "blank";
myComboBox9.Items.AddRange(new object[] {Ass6.TicTacToeGrid.BoxState.Blank,Ass6.TicTacToeGrid.BoxState.Cross,Ass6.TicTacToeGrid.BoxState.Naught});
myComboBox9.TabIndex = 8;
myComboBox9.SelectedIndexChanged += new EventHandler(this.ComboClick);
myComboBox9.Anchor =((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Controls.Add(myComboBox9);
this.ResumeLayout();
}
public void ComboClick(object sender, EventArgs e)
{
ComboBox tempControl = (ComboBox)sender;
TicTacToeGrid.BoxState tempSelected = new Ass6.TicTacToeGrid.BoxState();
//tempSelected = (TicTacToeGrid.BoxState)tempControl.SelectedValue;
tttGrid.ChangeBoard(tempControl.TabIndex,TicTacToeGrid.BoxState.Naught);
}
}
}
answered 9 months ago by:
0
THANKS!
answered 9 months ago by:
0
that was awfully nice of you rai
answered 9 months ago by:
0
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!