tic tac toe with variable table
-
Monday, June 11, 2012 6:03 AMHi dears. I wanna make a tic tac toe game in visual c# not consol aplication . i wanna have a variable table(Changeable table
). it means that at the first of the program the user enter the number of the columns and row of the table that he want . for example 4*4 or 10*10 . how should i make some thing like this . at the first i should design the UI of the program . please help me in how to design it ?- Edited by pooria_googooli Monday, June 11, 2012 6:16 AM
All Replies
-
Monday, June 11, 2012 7:23 AM
One way you can start is by:
1. Make a simple Form (say with just a label and Text box which asks user for Tic Tac Toe (TTT) matrix size - 3 for 3*3 etc)
2. Pass this value to a Child Form which contains a DataGridView. The DataGrid will add Columns and Rows according to this passed Value.
3. Ta Da you have your very very basic UI ready
.NET Maniac -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-
Monday, June 11, 2012 8:05 AM
Hi, I did an example code how it should look like
//program class: static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); int matrix = 0; using (Form2 f2 = new Form2()) { if (f2.ShowDialog() == DialogResult.OK) { matrix = f2.matrixNum; } } Application.Run(new Form1(matrix)); } } //form2 (to insert number of columns (rows -automatically): public partial class Form2 : Form { public int matrixNum { get; set; } public Form2() { InitializeComponent(); textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); this.AcceptButton = button1; } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { //allow number only and backspace if (e.KeyChar != '\b') { e.Handled = !char.IsNumber(e.KeyChar); } } private void button1_Click(object sender, EventArgs e) { this.matrixNum = int.Parse(textBox1.Text.Trim()); this.DialogResult = DialogResult.OK; } } //form1 (main form): public partial class Form1 : Form { DataTable table; public Form1() { InitializeComponent(); } public Form1(int matrix) : this() { table = new DataTable("TicTacToe"); for (int i = 0; i < matrix; i++) { table.Columns.Add(string.Format("col{0}", i), typeof(string)); table.Rows.Add(); } dataGridView1.Location = new Point(10, 10); dataGridView1.DataSource = table.DefaultView; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None; dataGridView1.ColumnHeadersVisible = false; dataGridView1.RowHeadersVisible = false; dataGridView1.AllowUserToAddRows = false; dataGridView1.AllowUserToDeleteRows = false; dataGridView1.AllowUserToOrderColumns = false; dataGridView1.AllowUserToResizeColumns = false; dataGridView1.AllowUserToResizeRows = false; dataGridView1.RowTemplate.Height = 100; //define size of columns and rows: int totalHeight = 0; int totalWidth = 0; for (int i = 0; i < dataGridView1.Columns.Count; i++) { totalHeight += 100; totalWidth += 100; dataGridView1.Columns[0].Width = 100; } dataGridView1.Size = new Size(totalWidth + 10, totalHeight + 10); this.Size = new Size(dataGridView1.Size.Width + 40, dataGridView1.Size.Height + 60); } }
Play a bit with setting DGV, I only did some example code.
bye
Mitja
-
Monday, June 11, 2012 8:28 AMThanks a lot . may you help me to use another thing . because DGV is not beautiful for this game . for example every time it draw some lines on the screen and make a table by drawing ?
-
Monday, June 11, 2012 9:05 AM
You could use labels to create the net. With some properties you can make label look like a line (horizontal or vertical).
Horizontal:
label1.AutoSize = false; label1.Text = null; label1.Height = 2; label1.BorderStyle = BorderStyle.Fixed3D; label1.Width = 100; //the "lenght" of the line
Vertical:
label1.AutoSize = false; label1.Text = null; label1.Height = 100;//the "lenght" of the line label1.BorderStyle = BorderStyle.Fixed3D; label1.Width = 2;
---
Now use this code to create labels depending on number or rows (and columns). This number would be "the number - 1" (if you have 3 columns you only need 2 vertical line (same for rows - horizontal lines)).
---
To help you out a bit more:
Create a loop (matrix -1) and create new label (2 on each step - 1st for horizontal, 2nd for vertical line).
Specify Location property (this one has to change for each label to create a net) - create some additonal method that will change varibles for X and Y axes for each label).
---
Then you can create buttons, to put them into each part of the net (if 3x3, means 9 buttons). Again you will have to locate depending on soem math calculations.
---
Hope it helps a bit, try now to do it. We cannt give you a fully working code (then what will be left to you?).
bye
Mitja
- Edited by Mitja BoncaMicrosoft Community Contributor Monday, June 11, 2012 9:06 AM
- Marked As Answer by pooria_googooli Monday, June 11, 2012 1:27 PM
-
Monday, June 11, 2012 1:29 PMThank you very much . it help me very much . if you know a website or weblog that explain more about creating this type of tic tac toe game please say me . thanks .

