Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do while, If, select case

Status
Not open for further replies.

Crazier

Programmer
Apr 24, 2002
10
US
can you use a do while then an if and a select case in one loop?
 
DIM Board$

FOR row = 1 TO 3
FOR col = 1 TO 3
PRINT Board$(col) + "| _ ";
NEXT col
PRINT "|"
NEXT row


INPUT "If you are the first player please type in X or O for the second player"; Player$
INPUT "Please type in 1 for the 1st position, 2 for the second etc."; position


DO WHILE Player$ <> Q$
IF Player$ = X$ THEN
SELECT CASE position
CASE 1
position = 1
LOCATE 1, 3
PRINT &quot;X&quot;
CASE 2
position = 2
LOCATE 1, 7
PRINT &quot;X&quot;
CASE 3
position = 3
LOCATE 1, 11
PRINT &quot;X&quot;
CASE 4
positon = 3
LOCATE 2, 3
PRINT &quot;X&quot;
CASE 5
position = 4
LOCATE 2, 7
PRINT &quot;X&quot;
CASE 6
position = 5
LOCATE 2, 11
PRINT &quot;X&quot;
CASE 7
position = 6
LOCATE 3, 3
PRINT &quot;X&quot;
CASE 8
position = 7
LOCATE 3, 7
PRINT &quot;X&quot;
CASE ELSE
position = 8
LOCATE 3, 11
PRINT &quot;X&quot;
END SELECT
ELSEIF Player$ = Q$ THEN
END
END IF

IF Player$ = O$ THEN
INPUT &quot;Please type in 1 for the 1st position, 2 for the second etc.&quot;; position
SELECT CASE position
CASE 1
position = 1
LOCATE 1, 3
PRINT &quot;O&quot;
CASE 2
position = 2
LOCATE 1, 7
PRINT &quot;O&quot;
CASE 3
position = 3
LOCATE 1, 11
PRINT &quot;O&quot;
CASE 4
positon = 3
LOCATE 2, 3
PRINT &quot;O&quot;
CASE 5
position = 4
LOCATE 2, 7
PRINT &quot;O&quot;
CASE 6
position = 5
LOCATE 2, 11
PRINT &quot;O&quot;
CASE 7
position = 6
LOCATE 3, 3
PRINT &quot;O&quot;
CASE 8
position = 7
LOCATE 3, 7
PRINT &quot;O&quot;
CASE ELSE
position = 8
LOCATE 3, 11
PRINT &quot;O&quot;
END SELECT
ELSEIF Player$ = Q$ THEN

END
END IF
LOOP

 
[flame] Hey, what do you expect? This person gave you more to go on than what I would have given you.

And I would have give you the straight answer:

YES (without any examples!)

Besides, have YOU even tried doing it before posting here? If you have and you are having problems with it, then post your code so that we can all take a look and try to point out what may be wrong. --MiggyD

Never be afraid to try something new. Remember that amateurs built the Ark. Professionals built the Titanic.
 
Yes I did try it first and I was having problems with my program, so I wanted to make sure that you could do the three things together. I try to work out my problems first before I get help and that was all I needed at the time and I thank you for your response.
 
Crazier,

Your [tt]SELECT CASE[/tt] blocks are not particularly well-structured, and it is not the right structure for the job in the first place. First of all, you are assigning to [tt]position[/tt] the value that it is already known to hold. The first line of each [tt]CASE[/tt] is roughly equivalent to:
[tt]
IF position = 5 THEN LET position = 5
[/tt]
Secondly, if you think of the math for a bit, you can calculate the coordinates to which to [tt]LOCATE[/tt]. Here is one way you could do it:
[tt]
LOCATE (position - 1) \ 3 + 1, ((position - 1) MOD 3) * 4 + 3
[/tt]
Here is why that works: When position is between 1 and 3, you want it to be on line 1, when it is between 4 and 6, you want line 2, and when it is between 7 and 9, you want line 3. The integer division operation '\' throws away the remainder of the division, giving only the integer quotient. Thus, 0 \ 3 = 1 \ 3 = 2 \ 3 = 0; 3 \ 3 = 4 \ 3 = 5 \ 3 = 1; 6 \ 3 = 7 \ 3 = 8 \ 3 = 2. Therefore, by subtracting one from [tt]position[/tt] and dividing it by 3, you get a number that goes up by one each time row changes, as you go through the positions in order. You then simply add to this the desired first row.

As for the other option, the [tt]MOD[/tt] operator returns the remainder that '\' throws away. The following table shows these values, as well as the desired column:
[tt]
x | x MOD 3 | column
---+---------+--------
0 | 0 | 3
1 | 1 | 7
2 | 2 | 11
3 | 0 | 3
4 | 1 | 7
5 | 2 | 11
6 | 0 | 3
7 | 1 | 7
8 | 2 | 11
[/tt]
As you can see, the values line up perfectly :) Since the difference between adjacent columns is 4, you multiply the result of the [tt]MOD[/tt] by 4. Then, when the [tt]MOD[/tt] expression returns 0, you want the column to be 3, so you add this on. The column is thus always equal to [tt](x MOD 3) * 4 + 3[/tt].

Finally, the PRINT statement for each [tt]CASE[/tt] always outputs the same value for each of the [tt][color] #000080]SELECT CASE[/color][/tt] blocks. So, you can replace each of the [tt]SELECT CASE[/tt] blocks with the following code, changing the &quot;X&quot; to &quot;O&quot; as appropriate:
[tt]
LOCATE (position - 1) \ 3 + 1, ((position - 1) MOD 3) * 4 + 3
PRINT &quot;X&quot;
[/tt]
 
Well done, logiclrd!
You'll always have a good Tip in your pocket and openmind others to new paths!!

Oak
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top