100% Guaranteed Results


PCS – Write a Tic Tac Toe game. Start with a simple version where one person can play another. Once you get that working, see if you can make a version where a person can play the computer. Solved
$ 24.99
Category:

Description

5/5 – (1 vote)

Some ideas on how to approach this:
1. Use a Form with a TableLayoutPanel of 3 rows and 3 columns
2. Fill the form with Labels. Keep a reference to each of these labels in a two dimensional Label array that is 3 * 3 (new Label[3, 3]) so that you have easy access to them and know which one is where.
3. Add a mouseClickListener to each Label. When clicked set the Text on the label to set it to X or O. You can either cycle between X and O each time a square is clicked, or keep track of whose turn it is and put in the correct symbol.
4. Each time a square is chosen call a method that checks to see if there was a winner. You can simply check all the possible winning combinations for the symbol just placed. You could do this hard coding all possible winning combinations, something like if (squares[0][0].Text.Equals(theSymbol) && squares[0][1].Text.Equals(theSymbol) && squares[0][2]Text.Equals(theSymbol)) – we have a winner!
or by using loops to be able to check multiple winning combinations without writing them all out.
5. To add some polish – put the TableLayoutPanel and its Lables into the center of the Form and create another panel that you put in the north or south that has buttons – one to restart (set all the label texts back to empty), and one to quit.
6. Once you get this working – see if you can make a version where the computer plays by calling a method to have the computer pick a square after processing the users click. You could simply keep on picking random spots until you find one that’s not taken (calling the label’s .Text and checking what’s there will tell you if a spot is taken). Or you could make it smarter – first try middle, then corners, etc…

Some useful methods and concepts:
1. To center the text in the Label cell.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
2. To get a bigger font, for bigger labels you can create a new Font with a bigger size. Something like this:
cell.Font = new System.Drawing.Font(“Segoe UI”, 27.75F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point);
3. You can show a message box to the user like this:
MessageBox.Show(this, symbol + ” is the winner”, “Tic Tac Toe”,MessageBoxButtons.OK,
MessageBoxIcon.Information);
The message box will be centered on whatever you pass as the first parameter, so if you pass your form, it will center it on the form. Passing null will (I think) center it on the screen.
4. To exit you can call Application.Exit();

Reviews

There are no reviews yet.

Be the first to review “PCS – Write a Tic Tac Toe game. Start with a simple version where one person can play another. Once you get that working, see if you can make a version where a person can play the computer. Solved”

Your email address will not be published. Required fields are marked *

Related products