Need help simplifying code, for loops, ifs and arrays
Author |
Message |
darkn00b
|
Posted: Wed Oct 21, 2009 3:57 pm Post subject: Need help simplifying code, for loops, ifs and arrays |
|
|
I want to simplify the 5th last line. There are a lot of ands in there, but only one number changes between each of them. I'm pretty sure this can be simplified, but I don't know how. I'm using the latest version of Turing, any other suggestions are welcome too. Thank you
Turing: |
setscreen ("graphics:640;480")
var font1, drawcount : int
var card, randomcard : array 0 .. 51 of int
font1 := Font.New ("serif:24")
assert font1 > 0
% make your 52 cards
for j : 0 .. 51
card (j ) := j
end for
drawcount := 0
%Randomize your cards
var random : int
for decreasing i : 51 .. 0
randint (random, 0, i )
randomcard (i ) := card (random )
card (random ) := card (i )
end for
%%%Draw cards
var carda : array 1 .. 7 of int
%Players cards
var count : int := 1
for i : 1 .. 2
Pic.ScreenLoad (intstr (randomcard (count )) + ".bmp", 180 + 90 * (i - 1), 380,
picCopy)
card (i ) := randomcard (count )
locate (i, 65)
if card (i ) mod 13 <= 9 and card (i ) mod 13 >= 1 then
put card (i ) mod 13 + 1. .
elsif card (i ) mod 13 = 10 then
put "Jack"..
elsif card (i ) mod 13 = 11 then
put "Queen"..
elsif card (i ) mod 13 = 12 then
put "King"..
elsif card (i ) mod 13 = 0 then
put "Ace"..
end if
if card (i ) div 13 = 0 then
put " of Hearts"..
elsif card (i ) div 13 = 1 then
put " of Spades"..
elsif card (i ) div 13 = 2 then
put " of Diamonds"..
elsif card (i ) div 13 = 3 then
put " of Clubs"..
end if
count := count + 1
end for
%Flop, turn and river
for i : 3 .. 7
Pic.ScreenLoad (intstr (randomcard (count )) + ".bmp", (maxx div 10) + (i - 2) * 80, maxy div 2 - 49,
picCopy)
card (i ) := randomcard (count )
locate (i, 65)
if card (i ) mod 13 <= 9 and card (i ) mod 13 >= 1 then
put card (i ) mod 13 + 1. .
elsif card (i ) mod 13 = 10 then
put "Jack"..
elsif card (i ) mod 13 = 11 then
put "Queen"..
elsif card (i ) mod 13 = 12 then
put "King"..
elsif card (i ) mod 13 = 0 then
put "Ace"..
end if
if card (i ) div 13 = 0 then
put " of Hearts"..
elsif card (i ) div 13 = 1 then
put " of Spades"..
elsif card (i ) div 13 = 2 then
put " of Diamonds"..
elsif card (i ) div 13 = 3 then
put " of Clubs"..
end if
count := count + 1
end for
var hcard : int := 0
for j : 1.. 7
if card (j ) mod 13 = 0 then
hcard := card (j )
exit
elsif card (j ) mod 13 >= card (1) mod 13 and card (j ) mod 13 >= card (2) mod 13 and card (j ) mod 13 >= card (3) mod 13 and card (j ) mod 13 >= card (4) mod 13 and card (j ) mod 13 >= card (5) mod 13 and card (j ) mod 13 >= card (6) mod 13 and card (j ) mod 13 >= card (7) mod 13 then
hcard := card (j )
end if
end for
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: Wed Oct 21, 2009 7:07 pm Post subject: Re: Need help simplifying code, for loops, ifs and arrays |
|
|
Hint: you can use a "else" and "for" and then "if" where you have the else-if. You can also use a function. |
|
|
|
|
|
darkn00b
|
Posted: Wed Oct 21, 2009 7:48 pm Post subject: RE:Need help simplifying code, for loops, ifs and arrays |
|
|
I tried splitting the elsif into an else, then a for loop (i: 1..7) and then the if, replacing the changing number with i, but that doesn't work because it has to be greater than or equal to all the values, not one at a time. I don't know how I could use a function in there either... |
|
|
|
|
|
TheGuardian001
|
Posted: Wed Oct 21, 2009 10:52 pm Post subject: Re: Need help simplifying code, for loops, ifs and arrays |
|
|
Well, if you need them all to be true and don't want to check them all at once all you need to do is keep track of whether or not they're all right. You could do that with a counter or a boolean, then just check the final value of said counter/boolean. |
|
|
|
|
|
darkn00b
|
Posted: Thu Oct 22, 2009 3:21 pm Post subject: RE:Need help simplifying code, for loops, ifs and arrays |
|
|
Thank you the guardian! I did the same thing that I said in my last post, except I added a counter and checked if the counter = 7. Thanks for the great help guys! |
|
|
|
|
|
|
|