5 Replies - 8588 Views - Last Post: 13 June 2008 - 09:05 AM Rate Topic: -----

#1 powellm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 08

tic tac toe game

Posted 11 June 2008 - 07:27 PM

I know there is alot of code for a tic tac toe game in visual basic but I am having a hard time understanding how to use visual basic so far in my game i need to get the x's and o's to work on the grid and finding out when some one has won.

Option Explicit
Dim intGamesPlayed As Integer
Dim intGamesLost As Integer
Dim intGamesWon As Integer
Dim intGamesTied As Integer
Dim intXPos As Integer
Dim intOPos As Integer                                                               'Variables declared Globally'
Dim strPlayerSign As String

Private Sub cmdPlay_Click()
    Dim intCounter As Integer
For intCounter = 0 To 8                     'Game cleared so New one can start'
    lblX(intCounter).Caption = ""
Next intCounter
End Sub

Private Sub cmdQuit_Click()
    Unload frmTictactoe
End Sub

Private Sub cmdStart_Click()
    Line1.Visible = True
    Line2.Visible = True
    Line3.Visible = True         'When Start Game Button is clicked objects appear'
    Line4.Visible = True
    lblTie.Visible = True
    lblWon.Visible = True
    lblPlayed.Visible = True
    lblLost.Visible = True
    cmdPlay.Visible = True
    cmdQuit.Visible = True
End Sub

Private Sub Form_Load()
    Dim intX As Integer
    
    Line1.Visible = False
    Line2.Visible = False
    Line3.Visible = False             'As the form opens objects are invisible'
    Line4.Visible = False
    lblTie.Visible = False
    lblWon.Visible = False
    lblPlayed.Visible = False
    lblLost.Visible = False
    cmdPlay.Visible = False
    cmdQuit.Visible = False
    
    For intX = 0 To 7
        lineThree(intX).Visible = False
    Next
    
    For intX = 0 To 8
        lblX(intX).Caption = ""
    Next
End Sub


Private Sub lblX_Click(Index As Integer)
   lblX(Index).Caption = strPlayerSign

If strPlayerSign = "X" Then
    strPlayerSign = "O"                   'X's and O's appear onto game board'
Else
    strPlayerSign = "X"
End If

End Sub



Mod Edit: Please use code tags when posting code, like :code:
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: tic tac toe game

#2 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

Re: tic tac toe game

Posted 11 June 2008 - 07:37 PM

Well the way I see it, you're gonna have to loop through all the boxes and make the program check for every possible way to win and see if any of those are fulfilled, which isn't too bad since there's only eight ways to win I believe.

What exact problem are you experiencing? Simply "It doesn't work" isn't good enough in a lot of cases.

By the way, remember to enclose your code in these tags:

[code ] CODE [/code ]

This post has been edited by Zhalix: 11 June 2008 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 powellm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 08

Re: tic tac toe game

Posted 11 June 2008 - 07:51 PM

the problem is when i select a box on the grid an x will appear but then if i were to click the same box again then a o would appear i want to know how i would go about making is so that a x would appear and the value would lock in then the next player would go selecting an o and that value would lock in and how to go about indentifying the win win 3 x's or o's would appear because i have the seven lines set up for the possible wins it would draw through but i do not know how to make so that if a win appears that line would be drawn across the winning values
Was This Post Helpful? 0
  • +
  • -

#4 Zhalix  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 260
  • Joined: 07-May 08

Re: tic tac toe game

Posted 11 June 2008 - 08:25 PM

:blink:

The reason the sign flips between "X" and "O" is because it's in the code to do that.

From looking at the code and your statements it seems you already have the lines prepared. Just, after checking the label control array to see if someone has won, make the correct line become visible by going 'Line#.Visible = True'.
Was This Post Helpful? 0
  • +
  • -

#5 powellm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-March 08

Re: tic tac toe game

Posted 12 June 2008 - 02:52 PM

can somebody walk me through in code on how to determine a win in a tic tac toe application
Was This Post Helpful? 0
  • +
  • -

#6 orangeslide8  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 203
  • Joined: 29-December 06

Re: tic tac toe game

Posted 13 June 2008 - 09:05 AM

You are using buttons right so when you click a button it changes if not I suggest you do
Ok so When i wrote my first tic tac toe game i thought the easiest way to make it so that the button you click doesn't switch a second time is to make it disabled. to do this after you change the text say you make button1 to be an x you would have this
button1.text = "X"
button1.enabled = false


secondly to see if someone won and who it was i would first use an array
so have an array like this
dim board(3,3)


then adding to the first code it should be like this
button1.text = "X"
board(1,1) = 1 'this is assuming that 1 is X and 0 is O
button1.enabled = false


then later you check i'll do the first one for you then you can try the next ones this is forcing it (no loop)
' first make a global variable at the top should look like this
dim player1win as boolean
dim palyer2win as boolean
' then later after each click changes call this sub
private sub Checkwin()
if board(1,1) = board(1,2) and board(1,2) = board(1,3) then
if board(1,1) = 1
player1win = ture
else
player2win = true
end if
' do it for all possibilities then your done


hope that helped
P.S. to make the entire game you should have a variable turn as integer the two win variables a string named string or what ever and an array.
when the form loads or the start button is clicked you should set string as "X" then in VB.Net on your form where you dragged buttons double click the button and put the code i showed you above except instead of = "X" write =string
then ones you set array and make the button disabled you should have
turn = turn + 1 
if int(turn/2)= turn/2 ' is it even 
string = "X" 
else
string = "O"
end if


or instead have only string as a variable and just again when the form loads or start button is clicked have it = "X" then instead of the above code have
if string = "X" then
string = "O"
else
string = "X"
end if


then at after that call the sub checkwin like this
checkwin()


at the end of checkwin have this check
if player1win = true or player2win = ture then
resetvariables()
end if


then have the last sub that is reset variables whihc puts them all back to 0 or "X"
hope that helped sorry for the long P.S.
John

This post has been edited by orangeslide8: 13 June 2008 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1