I need help with game.

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
jdpatel
Newbie
Posts:1
Joined:Sat May 13, 2006 6:08 am
I need help with game.

Post by jdpatel » Sat May 13, 2006 6:32 am

Well for school I have to make a Tic Tac Toe game, in which the computer either wins the game or ties it. Now I got the basics down, but, but I have a problem with placing in the right space. Like the my program will put it in a space that already has a "X" or a "O". I don't know how to make it so it checks before it puts it there, so if anyone could help. Here is the code:

Code: Select all

CLS
KEY OFF
SCREEN 9
COLOR 1

RANDOMIZE TIMER

10 LINE (210, 100)-(425, 100), 3
LINE (210, 160)-(425, 160), 3
LINE (285, 50)-(285, 205), 3
LINE (360, 50)-(360, 205), 3

LOCATE 10, 32: SC = 10: SC1 = 32
PRINT "1"
LOCATE 10, 41: SC = 10: SC1 = 41
PRINT "2"
LOCATE 10, 50: SC = 10: SC1 = 50
PRINT "3"
LOCATE 17, 32: SC = 17: SC1 = 32
PRINT "4"
LOCATE 17, 41: SC = 17: SC1 = 41
PRINT "5"
LOCATE 17, 50: SC = 17: SC1 = 50
PRINT "6"
LOCATE 24, 32: SC = 24: SC1 = 32
PRINT "7"
LOCATE 24, 41: SC = 24: SC1 = 41
PRINT "8"
LOCATE 24, 50: SC = 24: SC1 = 50
PRINT "9"



91 LOCATE 30, 10
PRINT "Please Choose Where You Want To Put The X At";
INPUT CH

100 IF CH = 1 THEN LOCATE 10, 32: PRINT "X": LOCATE 17, 41: PRINT "O"

101 LET WP = INT(4 * RND) + 1
102 IF WP = 1 THEN y = 10: x = 32
103 IF WP = 2 THEN y = 10: x = 50
104 IF WP = 3 THEN y = 17: x = 41
105 IF WP = 4 THEN y = 24: x = 41
IF CH = 2 THEN LOCATE 10, 41: PRINT "X": LOCATE y, x: PRINT "O"

IF CH = 3 THEN LOCATE 10, 50: PRINT "X": LOCATE 17, 41: PRINT "O"

106 LET WP = INT(4 * RND) + 1
107 IF WP = 1 THEN y = 10: x = 32
108 IF WP = 2 THEN y = 24: x = 32
109 IF WP = 3 THEN y = 17: x = 41
110 IF WP = 4 THEN y = 17: x = 50
IF CH = 4 THEN LOCATE 17, 32: PRINT "X": LOCATE y, x: PRINT "O"

120 LET WP = INT(4 * RND) + 1
IF WP = 1 THEN y = 10: x = 32
IF WP = 2 THEN y = 24: x = 32
IF WP = 3 THEN y = 24: x = 50
IF WP = 4 THEN y = 10: x = 50
IF CH = 5 THEN LOCATE 17, 41: PRINT "X": LOCATE y, x: PRINT "O"

121 LET WP = INT(4 * RND) + 1
122 IF WP = 1 THEN y = 10: x = 50
123 IF WP = 2 THEN y = 24: x = 50
124 IF WP = 3 THEN y = 17: x = 41
125 IF WP = 4 THEN y = 17: x = 32
IF CH = 6 THEN LOCATE 17, 50: PRINT "X": LOCATE y, x: PRINT "O"

IF CH = 7 THEN LOCATE 24, 32: PRINT "X": LOCATE 17, 41: PRINT "O"

130 LET WP = INT(4 * RND) + 1
131 IF WP = 1 THEN y = 24: x = 32
132 IF WP = 2 THEN y = 24: x = 50
133 IF WP = 3 THEN y = 17: x = 41
134 IF WP = 4 THEN y = 10: x = 41
IF CH = 8 THEN LOCATE 24, 41: PRINT "X": LOCATE y, x: PRINT "O"

IF CH = 9 THEN LOCATE 24, 50: PRINT "X": LOCATE 17, 41: PRINT "O"


140 LOCATE 32, 10
150 PRINT "Continue";
160 INPUT Z$
170 LET Z$ = INKEY$
180 IF INKEY$ = Z$ THEN GOTO 91

190 COUNT = 0
200 FOR SC = 10 TO 24 STEP 7
210 COUNT = 0
220 FOR SC1 = 32 TO 50 STEP 9
230 IF CH = SC AND CH = SC1 THEN CH = 1: COUNT = COUNT + 1
240 NEXT SC1
250 IF COUNT = 3 THEN PRINT "Player Wins": GOTO 340
260 NEXT SC


340 END


Ralph
QBasic God
Posts:134
Joined:Sat Nov 06, 2004 2:27 am
Location:Katy, Texas

Post by Ralph » Sun May 14, 2006 7:00 am

The following is one way to do what you need. Study it, and I'm sure that you will then be able to adapt it to your program, which seems quite good. You've made a good advance, so far. Keep it up. And, remember that the QuickBASIC Help contains just about everything you will ever need, so, study that, too.

Code: Select all

CLS
available$ = "123456789"

top:
PRINT "Choice "; available$;
INPUT ch$
b = INSTR(1, available$, ch$)
IF b = 0 THEN GOTO top 'if the digit chosen is not available, repeat


100
FOR i = 1 TO LEN(available$)
  a$ = MID$(available$, i, 1)
  IF a$ = ch$ THEN GOTO 104
  IF NewAvailable$ = "" THEN
    NewAvailable$ = a$
  ELSE
    NewAvailable$ = NewAvailable$ + a$
  END IF
104 :
NEXT i
available$ = NewAvailable$
NewAvailable$ = ""
IF available$ <> "" THEN
  GOTO top
END IF

PRINT " No more choices!"
WHILE INKEY$ = "": WEND
Ralph. Running QuickBASIC Version 4.5, Windows XP Home Edition, Version 2002, Service Pack 2, and HP LaserJet 4L printer.

Post Reply