Game

mickey2000
03-14-2001, 08:59 AM
I have some code here for mt tic tac toe game. How would I turn this code so that two live players can play, instead of a one player game against the computer? And if its a draw "Cat's gmae" should display. Also what would be the nicest looking form on how this game should look? (So that it doesnt look like a complicated mess).
Dim Turn As Integer
Sub NewGame()
For Index = 0 To 8
imgSquare(Index).Picture = imgBlank.Picture
Next
Turn = 0
End Sub
Sub Win()
MsgBox "You win!"
NewGame
End Sub
Sub Lose()
MsgBox "You lose!"
NewGame
End Sub
Private Sub imgSquare_Click(Index As Integer)
Randomize

If imgSquare(Index).Picture = None Then
imgSquare(Index).Picture = imgX.Picture
Turn = Turn + 1
If imgSquare(0).Picture = imgX.Picture And imgSquare(1).Picture = imgX.Picture And imgSquare(2).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(3).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(5).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(6).Picture = imgX.Picture And imgSquare(7).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(0).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(2).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(6).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(0).Picture = imgX.Picture And imgSquare(3).Picture = imgX.Picture And imgSquare(6).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(1).Picture = imgX.Picture And imgSquare(4).Picture = imgX.Picture And imgSquare(7).Picture = imgX.Picture Then Win: Exit Sub
If imgSquare(2).Picture = imgX.Picture And imgSquare(5).Picture = imgX.Picture And imgSquare(8).Picture = imgX.Picture Then Win: Exit Sub
If Turn < 5 Then
FindSquare:
Square = Int(9 * Rnd)
If imgSquare(Square).Picture = None Then
imgSquare(Square).Picture = imgO.Picture
Else
GoTo FindSquare
End If
If imgSquare(0).Picture = imgO.Picture And imgSquare(1).Picture = imgO.Picture And imgSquare(2).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(3).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(5).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(6).Picture = imgO.Picture And imgSquare(7).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(0).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(2).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(6).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(0).Picture = imgO.Picture And imgSquare(3).Picture = imgO.Picture And imgSquare(6).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(1).Picture = imgO.Picture And imgSquare(4).Picture = imgO.Picture And imgSquare(7).Picture = imgO.Picture Then Lose: Exit Sub
If imgSquare(2).Picture = imgO.Picture And imgSquare(5).Picture = imgO.Picture And imgSquare(8).Picture = imgO.Picture Then Lose: Exit Sub
End If
End If
If Turn = 5 Then
MsgBox "Game is drawn"
NewGame
End If
End Sub

Private Sub mnuNewGame_Click()
NewGame
End Sub

Private Sub mnuQuit_Click()
Unload frmTicTacToe
End Sub

mickey2000
03-14-2001, 09:40 AM
actually, I just changed all of my code and here it is attached. I do not know on how to add to the form or code that in case of the game is a tie or draw, that the program should display "Cat's Game." That is all that i need help with. Thanks to any one that can help

mickey2000
03-14-2001, 10:29 AM
i attached the wrong file. here is the correct one.

anhmytran
03-15-2001, 08:33 AM
The events of the game are the move of the players.
The events are not the timer, for the players may have
a break.

When there is a move, from any player, there is a check
to determin if there is a Win-Lose or a Tie-Draw.
The checks are Horizontal, Vertical and Diagonal.

So the identical design is a 2-D image control array.
However, I do not know how to make 2-D control Arrays.
I make 3 image control arrays (A, B and C) instead,
that makes the coding more complecate than 2-D control
arrays. I arrange image control arrays as a WorkSheet:
Each Control array has 3 elements from 1 to 3,
arranged vertically. Control contains no picture.
When a player clicks a control, it loads picture
from an invisible control O or X depending on the turn.
So the total image boxes are 11 (3 arrays and 2 loaded).

At run time, at a move (an element is clicked) it checks
if there has been a Win or a Tie.
Then it loads picture to the current element with O or X.
Then it checks vertically, horizontally and diagonally
for a Win.
Then it checks whether all elements of 3 arrays are loaded
with picture to determin whether a Tie.

The following is the source code. It has some bugs, for
I do not spend enough time on it. Please, check it out.
*************
Option Explicit
Dim OorX As Boolean ' True: O's turn; False: X's turn
Dim intOScore As Integer, intXScore As Integer
Dim Win As Boolean ' When any of them wins
Dim Tie As Boolean ' When there is a Tie


Private Sub A_Click(Index As Integer)
' do nothing unless user restart
If Win Or Tie Then
MsgBox "Please, restart", vbOKOnly, "AnhMy Tran"
ClearBoard
Win = False
Tie = False
Exit Sub
End If

' Check if O wins, for it is O's turn
If OorX = True Then
' Load O picture to the current Image box
A(Index).Picture = O.Picture
' Check vertically if O wins
If A(1).Picture = O.Picture And A(2).Picture = O.Picture _
And A(3).Picture = O.Picture Then
OWins
Exit Sub
End If
Else:
' Check if X wins, for it is O's turn
' Load X picture to the current Image box
A(Index).Picture = X.Picture
' Check vertically if X wins
If A(1).Picture = X.Picture And A(2).Picture = X.Picture _
And A(3).Picture = X.Picture Then
XWins
Exit Sub
End If
End If

' Check diagonally if any wins
If Index = 1 Or Index = 3 Then
CheckDiagonal OorX
If Win = True Then
Exit Sub
End If
' Check horizontally if any wins
ElseIf Index = 2 Then
CheckHorizontal OorX, Index
If Win = True Then
Exit Sub
End If
End If

' Switch the turn for the next round
OorX = Not OorX
' Check whether there is a Tie
CheckTie
End Sub


Private Sub B_Click(Index As Integer)
' do nothing unless user restart
If Win Or Tie Then
MsgBox "Please, restart", vbOKOnly, "AnhMy Tran"
ClearBoard
Win = False
Tie = False
Exit Sub
End If

' Check if O wins, for it is O's turn
If OorX = True Then
' Load picture to the current Image box
B(Index).Picture = O.Picture
' Check vertically if O wins
If A(1).Picture = O.Picture And A(2).Picture = O.Picture _
And A(3).Picture = O.Picture Then
OWins
Exit Sub
End If
Else:
' Check if X wins, for it is X's turn
' Load X picture to the current Image box
B(Index).Picture = X.Picture
' Check vertically if X wins
If B(1).Picture = X.Picture And B(2).Picture = X.Picture _
And B(3).Picture = X.Picture Then
XWins
Exit Sub
End If
End If

' Check diagonally if any wins
If Index = 2 Then
CheckDiagonal OorX
If Win = True Then
Exit Sub
End If
' Check horizontally if any wins
ElseIf Index = 1 Or Index = 3 Then
CheckHorizontal OorX, Index
If Win = True Then
Exit Sub
End If
End If

' Switch the turn for the next round
OorX = Not OorX
' Check whether there is a Tie
CheckTie

End Sub


Private Sub C_Click(Index As Integer)
' do nothing unless user restart
If Win Or Tie Then
MsgBox "Please, restart", vbOKOnly, "AnhMy Tran"
ClearBoard
Win = False
Tie = False
Exit Sub
End If
' Check if O wins, for it is O's turn
If OorX = True Then
' Load O picture to the current Image box
C(Index).Picture = O.Picture
' Check vertically if O wins
If C(1).Picture = O.Picture And C(2).Picture = O.Picture _
And C(3).Picture = O.Picture Then
OWins
Exit Sub
End If
Else:
' Check if X wins, for it is X's turn
' Load X picture to the current Image box
C(Index).Picture = X.Picture
' Check vertically if X wins
If C(1).Picture = X.Picture And C(2).Picture = X.Picture _
And C(3).Picture = X.Picture Then
XWins
Exit Sub
End If
End If

' Check diagonally if any wins
If Index = 1 Or Index = 3 Then
CheckDiagonal OorX
If Win = True Then
Exit Sub
End If
' Check horizontally if any wins
ElseIf Index = 2 Then
CheckHorizontal OorX, Index
If Win = True Then
Exit Sub
End If
End If

' Switch the turn for the next round
OorX = Not OorX
' Check whether there is a Tie
CheckTie
End Sub


Private Sub CheckDiagonal(OX As Boolean)
If OX = True Then
' Check diagonally if O wins
If (B(2).Picture = O.Picture And A(1).Picture = O.Picture And _
C(3).Picture = O.Picture) Or _
(B(2).Picture = O.Picture And A(3).Picture = O.Picture And _
C(1).Picture = O.Picture) Then
OWins
End If
Else:
' Check diagonally if X wins
If (B(2).Picture = X.Picture And A(1).Picture = X.Picture And _
C(3).Picture = X.Picture) Or _
(B(2).Picture = X.Picture And A(3).Picture = X.Picture And _
C(1).Picture = X.Picture) Then
XWins
End If
End If
End Sub


Private Sub CheckHorizontal(OX As Boolean, Idx As Integer)
If OX = True Then
' Check Horizontally if O wins
If A(Idx).Picture = X.Picture And B(Idx).Picture = X.Picture And _
C(Idx).Picture = X.Picture Then
XWins
End If
Else:
' Check Horizontally if X wins
If A(Idx).Picture = X.Picture And B(Idx).Picture = X.Picture And _
C(Idx).Picture = X.Picture Then
XWins
End If
End If
End Sub

Private Sub CheckTie()
' This procedure checks whether the game is tie
Dim i As Integer, intTotalPic As Integer
For i = 1 To 3
If A(i).Picture = O.Picture Or A(i).Picture = X.Picture Then
intTotalPic = intTotalPic + 1
End If
If B(i).Picture = O.Picture Or B(i).Picture = X.Picture Then
intTotalPic = intTotalPic + 1
End If
If C(i).Picture = O.Picture Or C(i).Picture = X.Picture Then
intTotalPic = intTotalPic + 1
End If
Next i
If intTotalPic = 9 And Win = False Then
Tie = True
MsgBox "Game Tie", vbOKOnly, "AnhMy Tran"
End If
End Sub


Private Sub ClearBoard()
Dim i As Integer
For i = 1 To 3
' Clear all images
A(i).Picture = LoadPicture()
B(i).Picture = LoadPicture()
C(i).Picture = LoadPicture()
Next i
End Sub


Private Sub Form_Load()
Win = False
Tie = False
End Sub


Private Sub cmdExit_Click()
End
End Sub

Private Sub OWins()
MsgBox "O wins", vbOKOnly, "AnhMy Tran"
intOScore = intOScore + 1
lblO.Caption = CStr(intOScore)
OorX = Not OorX
Win = True
End Sub


Private Sub XWins()
MsgBox "X wins", vbOKOnly, "AnhMy Tran"
intXScore = intXScore + 1
lblX.Caption = CStr(intXScore)
OorX = Not OorX
Win = True
End Sub


Private Sub cmdStart_Click()
' This procedure resets all values to get ready

ClearBoard ' clear all loaded images
' Reset all scores as Zero
intOScore = 0
intXScore = 0
' Display the scores as Zeros
lblO.Caption = "0"
lblX.Caption = "0"

End Sub

Private Sub optO_Click()
cmdStart_Click
OorX = True ' it is O to advance
Win = False
Tie = False
End Sub

Private Sub optX_Click()
cmdStart_Click
OorX = False ' it is X to advance
Win = False
Tie = False
End Sub
*************

AnhMy_Tran

klabranche
03-15-2001, 08:47 AM
you could also just use a 2-D array and that would work just as well but testing would be easier.


http://www.extreme-vb.net/images/icons/smile.gif

BillSoo
03-15-2001, 03:32 PM
I think the problem is that you can't create a 2 dimensional control array. But you could just make a one dimensional array and use pointer arithmetic.

image((row-1) * 3 + col)

where image runs from 1 to 9, row from 1 to 3 and col from 1 to 3

Or if you like option base 0, then

image(row * 3 + col)

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum