Tracking a player's turn in Tic-Tac-Toe?

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 4083 Views - Last Post: 13 April 2011 - 04:05 PM Rate Topic: -----

#1 gareth.nic  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 102
  • Joined: 22-October 10

Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 06:44 AM

Hi

For a small project at college I have to create a simple tic-tac-toe game. I'm using a windows form instead of the console and my grid is made up of 3x3 buttons. I've got the method nailed down on checking winner/loser/draw.

But I don't know how to make the application switch to player 2 after player 1 has clicked a button in the grid and vice versa. if player 1 is X and clicks any button in the grid, it should be X. That button should then be disabled and the program waits for player 2 to click.

Don't know if it's like writer's block, but I just can't get my head wrapped around the code.

Any advice would be appreciated.

This post has been edited by gareth.nic: 10 April 2011 - 06:45 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Tracking a player's turn in Tic-Tac-Toe?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 06:59 AM

We can't just give you code for your homework.
You need to at least show us your good faith effort to code this so far.

Personally I would have a string for the player symbol, then switch it to the other player symbol.

string Player = "X";

void SwitchPlayer()
{
  Player = Player=="X" ? "O" : "X";
}


But that may not mesh well with your existing code.
Was This Post Helpful? 4
  • +
  • -

#3 gareth.nic  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 102
  • Joined: 22-October 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 10:06 AM

I understand. The method check() is used to check who wins every time a button in the grid is pressed. I'd post the whole method, but it's filled with the same code only checking the different possibilities.



public void check()
        {
            if (btn1.Text == "X")
            {
                if (btn2.Text == "X")
                {
                    if (btn3.Text == "X")
                    {
                        lblPlayer1Score.Text = "1"; // going to add the score counter here later.
                        MessageBox.show("Player 1 wins!");
                    }
                }
            }

        }

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 10:54 AM

Is it really your intention to manually check every possible winning condition for X and then again for O through a set of nested 'if' conditions like that?

Here's a hint. There are only 8 possible conditions you need to check for either player. And you can use the same method to run them by passing in the the player symbol as a parameter. The entire check() method should only have to be about 10 lines.
Was This Post Helpful? 2
  • +
  • -

#5 gareth.nic  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 102
  • Joined: 22-October 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 11:46 AM

Yeah, I see what you mean. Less lines are my goal. They've been teaching us the long way of doing things for a year and I guess I'm starting to think like that. I'm going to try and get only 10 lines.

I tried your code, it worked :) Thanks

Just gotta work it back to the long way...

This post has been edited by gareth.nic: 10 April 2011 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 11:58 AM

You don't need 9 method handlers for 9 buttons. They can all be sent to the same single handler. Remember the button you click on is sent to the handler as the "object sender"

Ok.. I'm really bored and don't feel like working on work.

Its' 1400hrs. I'm going to start a new project for TickTackToe and see how long it takes. See ya shortly.
Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 12:29 PM

1420 hours - So 20 minutes to write the game, another 5-8 to take screenshots and write this reply.

86 lines which includes all the #region markers

Attached Image

Attached Image

This uses the advice I gave you about having all of the buttons use the same handler method.
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 12:40 PM

Quote

They've been teaching us the long way of doing things for a year


Stop... You've been in this course for a full year... and they have you coding like this...

Quote

public void check()
        {
            if (btn1.Text == "X")
            {
                if (btn2.Text == "X")
                {
                    if (btn3.Text == "X")
                    {
                        lblPlayer1Score.Text = "1"; // going to add the score counter here later.
                        MessageBox.show("Player 1 wins!");
                    }
                }
            }
        }



To me it seems like they are ripping you off or stretching out the course for as long and slow as they can for some reason. Either they want you taking 8 years of courses for the money they make, or the instructor has only 1 year of knowledge to pass along 4 years of courses.

I would suggest you buy a book or 3, and do some on-line tutorials. You could have taught yourself far more in far less time for a lot less money from any one of a number of "Teach yourself C# in 30 days" type books.


Standard resources, references and suggestions for new programmers.

I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

C# resources. Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Quick and easy custom events
Bulding an application - Part 1
Building an application - Part 2
Passing values between forms/classes
Debugging tutorial
Working with environmental variables

Debugging tutorial
Debugging tips
Great debugging tips

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.


These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on

Was This Post Helpful? 1
  • +
  • -

#9 gareth.nic  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 102
  • Joined: 22-October 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 01:31 PM

WOW okay, so that just opened up my eyes. You've got 86 lines and I'm just over 300. That makes me agree with your statement about the course...

Thanks for the help and eye opener. I'll definitely be checking out all the links you've posted!
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 01:41 PM

View Postgareth.nic, on 10 April 2011 - 02:31 PM, said:

WOW okay, so that just opened up my eyes. You've got 86 lines and I'm just over 300.


Oh hellllllll no.

I don't believe in giving students full code for homework. Neither does the DIC site. That is because we have a responsibility to the student to make them actually work for their education.

But you don't seem to be getting an education at your school. Either you or the teacher needs a Gibbs slap to come back to reality. Maybe if you turn in a better formed application the instructor will realize he needs to start working for a living again. Or you can have an example of how to better design your programs. One assignment isn't going to magically boost your entire grade, so hopefully someone doesn't pull this down.

Of course of you turn this in as your work without understanding it, you're screwed when it comes question and answer time. And your future assignments will need to meet this level of work or you're busted. So study what you don't understand.

Spoiler

This post has been edited by tlhIn`toq: 10 April 2011 - 01:43 PM

Was This Post Helpful? 2
  • +
  • -

#11 gareth.nic  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 102
  • Joined: 22-October 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 10 April 2011 - 02:42 PM

Yeah, that's what I was thinking when I saw the line count. The thing is, most of our class just finished high school and you can tell they just took the course because they think there's money to be made after it. But they're generally not that much computer literate.

That said, the lecturer knows me and a couple other guys have a little VB experience (also taught from the same school with teachers barely speaking English) - and he decided to give us this tic-tac-toe application. Yet we get taught the same amount of stuff as the others, being held back by others has never been such a b*^th.

Quote

Of course of you turn this in as your work without understanding it, you're screwed when it comes question and answer time. And your future assignments will need to meet this level of work or you're busted. So study what you don't understand.


I fully understand that. That's why I don't believe in using other people's code just as it is. I'm just going to use the code you posted as a guideline and write it my way. That way I'll know exactly what the code is doing and what it means.

Thanks for all your help.

This post has been edited by gareth.nic: 10 April 2011 - 02:43 PM

Was This Post Helpful? 1
  • +
  • -

#12 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 11 April 2011 - 08:01 AM

probably one of the best newb posts I've seen here in a good while.

Top notch fellows.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 11 April 2011 - 08:07 AM

Thanks. It actually pointed me to a new tutorial I think I will do about planning out the architecture first and highlight how many controls can point to the same method handler. It seems a lot of people aren't learning that approach. Something of a 'Techniques to make your program less complicated' type article.
Was This Post Helpful? 0
  • +
  • -

#14 Infinite loop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 04-April 11

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 12 April 2011 - 09:09 AM

I would just like to recommend that you could just check the three possible end game solutions for the button clicked. So if you assign the buttons a grid you would check the Col of the same symbol, then check the row. Then if the Button is on a / \ check for that solution condition. That way you would only need four checks. Not sure the lines I have not rewritten the Check Code yet. It would save processing time and possibly reduce the number of lines as well.
Was This Post Helpful? 0
  • +
  • -

#15 gareth.nic  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 102
  • Joined: 22-October 10

Re: Tracking a player's turn in Tic-Tac-Toe?

Posted 12 April 2011 - 09:23 AM

View PosttlhIn`toq, on 11 April 2011 - 05:07 PM, said:

Thanks. It actually pointed me to a new tutorial I think I will do about planning out the architecture first and highlight how many controls can point to the same method handler. It seems a lot of people aren't learning that approach. Something of a 'Techniques to make your program less complicated' type article.


I second that! It'll definitely save a bunch of unnecessary lines.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2