Rock-paper-scissors: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 4,515: Line 4,515:
# Project : Rock-paper-scissors
# Project : Rock-paper-scissors


load "stdlib.ring"
see "
load "guilib.ring"
welcome to the game of rock-paper-scissors.

each player guesses one of these three, and reveals it at the same time.
width = 200
rock blunts scissors, which cut paper, which wraps stone.
height = 200
if both players choose the same, it is a draw!

when you've had enough, choose q.
myChose = 1
"
compChose = 1
g = ["rock","paper","scissors"]
nextPlayer = 1
total=0 draw=0
myScore = 0
pwin=0 cwin=0
compScore = 0
see "what is your move (press r, p, or s)?"
C_FONTSIZE = 15
while true

c=random(2)+1
C_ROCK = "images/rock.jpg"
give q
C_PAPER = "images/paper.jpg"
gs = floor((substr("rpsq",lower(q))))
C_SCISSORS = "images/scissors.jpg"
if gs>3 or gs<1

summarise()
ChoseList = [C_ROCK,C_PAPER,C_SCISSORS]
exit

ok
Button = list(len(ChoseList))
total = total + 1

see"you chose " + g[gs] + " and i chose " + g[c] + nl
app = new QApp
temp = gs-c
{
if temp = 0

see ". it's a draw"
StyleFusion()
draw = draw + 1

ok
win = new QWidget() {
if temp = 1 or temp = -2

see ". you win!"
setWindowTitle('Stone Paper Scissors Game')
pwin = pwin + 1
setWinIcon(self,C_ROCK)
ok
setStyleSheet("background-color:cyan;")
if temp = (-1) or temp = 2
setWindowFlags(Qt_Window | Qt_WindowTitleHint | Qt_WindowCloseButtonHint | Qt_CustomizeWindowHint)
see ". i win!"
reSize(900,600)
cwin = cwin + 1
winheight = height()
ok
fontSize = 8 + (winheight / 100)
end

</lang>
for Col = 1 to len(ChoseList)
Output:
Button[Col] = new QPushButton(win) {
<pre>
x = 150+(Col-1)*height
welcome to the game of rock-paper-scissors.
setgeometry(x,35,width,height)
each player guesses one of these three, and reveals it at the same time.
setStyleSheet("background-color:white;")
rock blunts scissors, which cut paper, which wraps stone.
seticon(new qicon(new qpixmap(ChoseList[Col])))
if both players choose the same, it is a draw!
setIconSize(new qSize(200,200))
when you've had enough, choose q.
setclickevent("ButtonPress(" + string(Col) + ")")
setSizePolicy(1,1)
}
next

labelMyChose = new QLabel(win) {
setgeometry(200,250,150,30)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("My Chose:")
}

labelCompChose = new QLabel(win) {
setgeometry(580,250,150,30)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("Comp Chose:")
}

labelScoreEnd = new QLabel(win) {
setgeometry(0,510,win.width(),30)
setAlignment(Qt_AlignHCenter | Qt_AlignVCenter)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("")
}

btnMyChose = new QPushButton(win) {
setgeometry(150,300,width,height)
setStyleSheet("background-color:white;")
}

btnCompChose = new QPushButton(win) {
setgeometry(550,300,width,height)
setStyleSheet("background-color:white;")
}

btnNewGame = new QPushButton(win) {
setgeometry(170,550,150,40)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
setclickevent("NewGame()")
settext("New Game")
}

btnExit = new QPushButton(win) {
setgeometry(580,550,150,40)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
setclickevent("Close()")
settext("Exit")
}

labelMyScore = new QLabel(win) {
setgeometry(170,0,100,30)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("My Score: ")
}

labelMyScoreSum = new QLabel(win) {
setgeometry(300,0,100,30)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("")
}

labelCompScore = new QLabel(win) {
setgeometry(580,0,130,30)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("Comp Score: ")
}

labelCompScoreSum = new QLabel(win) {
setgeometry(730,0,100,30)
setFont(new qFont("Verdana",C_FONTSIZE,50,0))
settext("")
}

show()

}

exec()

}

func ButtonPress Col

if nextPlayer = 1
myChose = Col
btnMyChose {
seticon(new qicon(new qpixmap(ChoseList[Col])))
setIconSize(new qSize(width,height))
}
nextPlayer = 2
compChose()
ok

func compChose

rndChose = random(len(ChoseList)-1) + 1
compChose = rndChose
btnCompChose {
seticon(new qicon(new qpixmap(ChoseList[compChose])))
setIconSize(new qSize(width,height))
}
nextPlayer = 1
Result()

func Result

if (myChose = compChose)
labelScoreEnd.settext("Draw!")
ok
if (myChose = 1) and (compChose = 2)
labelScoreEnd.settext("Computer Win!")
compScore = compScore + 1
labelCompScoreSum.settext(string(compScore))
ok
if (myChose = 1) and (compChose = 3)
labelScoreEnd.settext("I Win!")
myScore = myScore + 1
labelMyScoreSum.settext(string(myScore))
ok
if (myChose = 2) and (compChose = 3)
labelScoreEnd.settext("Computer Win!")
compScore = compScore + 1
labelCompScoreSum.settext(string(compScore))
ok
if (myChose = 2) and (compChose = 1)
labelScoreEnd.settext("I Win!")
myScore = myScore + 1
labelMyScoreSum.settext(string(myScore))
ok
if (myChose = 3) and (compChose = 1)
labelScoreEnd.settext("Computer Win!")
compScore = compScore + 1
labelCompScoreSum.settext(string(compScore))
ok
if (myChose = 3) and (compChose = 2)
labelScoreEnd.settext("I Win!")
myScore = myScore + 1
labelMyScoreSum.settext(string(myScore))
ok

func NewGame

nextPlayer = 1
myScore = 0
compScore = 0
btnMyChose {
seticon(new qicon(new qpixmap("")))
setIconSize(new qSize(200,200))
}

btnCompChose {
seticon(new qicon(new qpixmap("")))
setIconSize(new qSize(200,200))
}

labelScoreEnd.settext("")
labelMyScoreSum.settext("0")
labelCompScoreSum.settext("0")

func Close

win.close()
app.quit()


what is your move (press r, p, or s)?
r
you chose rock and i chose rock
. it's a draw
p
you chose paper and i chose paper
. it's a draw
s
you chose scissors and i chose rock
. i win!
R
you chose rock and i chose paper
. i win!
P
you chose paper and i chose paper
. it's a draw
S
you chose scissors and i chose paper
. you win!
q
you won 1, and i won 2. there were 3 draws
thanks for playing!
</pre>
</pre>