Tic tac toe old project

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 12560 Views - Last Post: 18 March 2013 - 02:24 PM Rate Topic: -----

#1 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Tic tac toe old project

Posted 16 March 2013 - 01:42 PM

Hi, i was kinda bored and decided to play with old school projects i got, and then i was viewing my tic-tac-toe code and i wanted to know something... how can i make an "tie Msgbox"? I was displaying the draw messagebox with a timer and i think that's kinda bad, and i'd like to know how to do it properly, like checking if all buttons are pressed, then if no messagebox appears (The messagebox that displays who won) it would display a draw-one...

Is that possible?
I'm not sure how to do it without envolving other variables, can't be done this way?

Thanks for anyone who helps.

Is This A Good Question/Topic? 0
  • +

Replies To: Tic tac toe old project

#2 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 285
  • Posts: 893
  • Joined: 01-February 13

Re: Tic tac toe old project

Posted 16 March 2013 - 01:50 PM

Hi HeBTeMy,
I am sure it probably could be done somehow but, without seeing your old code it is hard to say How or If it could be done. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Tic tac toe old project

Posted 16 March 2013 - 01:55 PM

In the button click event, you could check to see if any buttons are left.

Not sure how you wrote it but I would set a While Loop that monitors a GameOver boolean or something similar

Combine the handles of your buttons and use the Sender object to determine if available and mark the current player's symbol.
Check for win by comparing = symbols (you don't need to check for x and check for o just do it all at once by seeing if the text is =
if it's a win then set GameOver = true otherwise, check if any buttons are left if neither is true switch to the other player for their turn.

Outside the loop, Display the winner or tie game. Ask to play again.
Was This Post Helpful? 0
  • +
  • -

#4 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Tic tac toe old project

Posted 16 March 2013 - 03:07 PM

I wrote it in the first year of my school, so, it's pretty "noob"...

It's like this:

 If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then
            MsgBox("O " + Form2.TextBox1.Text + "  o vencedor", MsgBoxStyle.Exclamation)
            vit1 = vit1 + 1
            Label1.Text = Form2.TextBox1.Text & " : " & vit1
            Label7.Visible = True
            Timer1.Stop()
            conta = conta + 1
            Button1.Enabled = False
            Button2.Enabled = False
            Button3.Enabled = False
            Button4.Enabled = False
            Button5.Enabled = False
            Button6.Enabled = False
            Button7.Enabled = False
            Button8.Enabled = False
            Button9.Enabled = False
        End If



And i did it for all the possibilities C+P'ed for the other buttons... yeah, it's pretty "lame" but it was what i knew at the time..
the msgbox says who won and i got a button to play again in the form, and it's basically it.
Vit is the number os victories for each player (vit1/vit2) conta is the % of wins overall between the two players.
i have a timer wich checks for a "thing" that i've putted in every piece of code above and if it was false then it would retrieve an "draw" messagebox.

I was asking another way to do it without making that random checks in every button.
Appreciate it.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Tic tac toe old project

Posted 16 March 2013 - 03:45 PM

So have you learned a lot more since you wrote that? New methods that would reduce that code?

If you're bored, it would be a good exercise to try to reduce repetitive code and learn new ways to accomplish what you're doing.
Was This Post Helpful? 0
  • +
  • -

#6 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Tic tac toe old project

Posted 16 March 2013 - 04:07 PM

You may say so :D... i've not learn that much but i know it can be reduced to 200 lines or less the whole project instead of the 2000 or something i have..

But this moment i was in need of accomplish that problem i've asked for a friend of mine who ask me for help for school too, i don't wanna give him my "timer-crap-invented" that i come up with xD

But the best way of reducing it would be some kind of arrays? I've seen that somewhere, can't remember well..
Was This Post Helpful? 0
  • +
  • -

#7 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Tic tac toe old project

Posted 16 March 2013 - 07:40 PM

How do you know it can be reduced to 200 lines or less if you dont know which code structure are you going to use to make it better?
Was This Post Helpful? 0
  • +
  • -

#8 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Tic tac toe old project

Posted 17 March 2013 - 02:16 PM

Because i remember my teacher saying something like that...
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Tic tac toe old project

Posted 17 March 2013 - 07:42 PM

View PostJondeate, on 17 March 2013 - 03:40 AM, said:

How do you know it can be reduced to 200 lines or less

We know because some of us have done it. If you find you are writing similar looking sections of code, maybe it can be extracted out into it own method. Then call that.

If you don't know about basic collections, or about reference types then do same basic independent study or research. There are tutorials on Dream.In.Code that cover those topics.



Ignore the GUI aspect of the problem (trust me it helps.)

Begin by thinking about the design (or the flow of the game) of the program. Use pencil and paper.

For example
 Blank board
 Current Player = Player 1
 While the game is not in a end state
   Await Current Player Move
   Update Board with Current Player's Move
   Check State of Board
     Has Current Player Won? -> End Game( Player (Current Player ) Wins)
     Is the Game Drawn?      -> End Game( Drawn Game )
   Current Player = Which Player is Next? ( Current Player )
 End While



Now I would go and design a class (or classes) that encapsulates the playing of a game of tic-tsc-toe. I just done this as an exercise and it's about 120 LoC (and it is GUI neutral, when compiled it 90 IL LoC) .

This post has been edited by AdamSpeight2008: 17 March 2013 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#10 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Tic tac toe old project

Posted 18 March 2013 - 12:13 PM

I have most of that "functions", including the move count, but how do i do it?
Like: If count = 9 then
...?

I can't put game drawn because the last move still can be someone's victory...
Should i use Exit sub in every possibilitie of Victory to stop from reaching my draw statement if true?

This post has been edited by AdamSpeight2008: 18 March 2013 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Tic tac toe old project

Posted 18 March 2013 - 12:47 PM

Let's start with the simplest conceptually. How are you going check for a drawn game?
Write it out an English statement that describes a drawn game.
Was This Post Helpful? 0
  • +
  • -

#12 HeBTeMy  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Tic tac toe old project

Posted 18 March 2013 - 01:06 PM

View PostAdamSpeight2008, on 18 March 2013 - 12:47 PM, said:

Let's start with the simplest conceptually. How are you going check for a drawn game?
Write it out an English statement that describes a drawn game.


That was primarly my question... i was looking of a simple way to do it as i used to do it with timers and ramdom changes to check...
If i don't find an answer (easy one) i will have to find "other" way checking "random" things like label.text changed or something.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2263
  • View blog
  • Posts: 9,467
  • Joined: 29-May 08

Re: Tic tac toe old project

Posted 18 March 2013 - 01:28 PM

Stop thinking about vb.net code for moment, and think conceptually.
In my written example (in a prior) do you see any actual code?
Spoiler


For example create and define the definition you are going to use, for different aspects of the game.

Spoiler

This post has been edited by AdamSpeight2008: 18 March 2013 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#14 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 285
  • Posts: 893
  • Joined: 01-February 13

Re: Tic tac toe old project

Posted 18 March 2013 - 01:30 PM

Sorry for butting in but, cant you just use an If ElseIf if you don`t want to change the way you have the game set up already and don`t want to use any more variables as you said in other posts ? :unsure:/>
        If Button1.Text = "X" And Button2.Text = "X" And Button3.Text = "X" Then

            'If player wins it does this

        ElseIf Count = 9 Then

            'If Tie Game it does this

        End If


This post has been edited by IronRazer: 18 March 2013 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Tic tac toe old project

Posted 18 March 2013 - 01:37 PM

Look at the change I made to AdamSpeight2008's Definition. I think you're getting confused here, so I added a little something to clarify what I think you're having trouble with.

Quote

Definition: Game Play

A player places his marker into one of the empty squares
If is it a winning move then they have won the game
But if it's not determined that anyone has won, then
If all the board squares are occupied then the game is drawn
Otherwise it is the other player's turn.

So a player set's their square: Did a win happen? If Yes then show who won: Game Over. If No, then check to see if there are any squares left for the other player. If not, you're already determined there wasn't a win so it's a draw: Game Over

This post has been edited by CharlieMay: 18 March 2013 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2