How to resolve the algorithm Rock-paper-scissors step by step in the Ring programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Rock-paper-scissors step by step in the Ring programming language

Table of Contents

Problem Statement

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player. Rock Paper Scissors is a two player game. Each player chooses one of rock, paper or scissors, without knowing the other player's choice. The winner is decided by a set of rules:

If both players choose the same thing, there is no winner for that round. For this task, the computer will be one of the players. The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.

Support additional choices   additional weapons.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Rock-paper-scissors step by step in the Ring programming language

Source code in the ring programming language

# Project : Rock-paper-scissors

load "stdlib.ring"
load "guilib.ring"

width 		= 200
height 		= 200

myChose 	= 1
compChose 	= 1
nextPlayer 	= 1
myScore 	= 0
compScore 	= 0
C_FONTSIZE 	= 15

C_ROCK 		= "images/rock.jpg"
C_PAPER 	= "images/paper.jpg"
C_SCISSORS 	= "images/scissors.jpg"

ChoseList 	= [C_ROCK,C_PAPER,C_SCISSORS]

Button 		= list(len(ChoseList))

app = new QApp 
{

	StyleFusion()

	win = new QWidget() {

		setWindowTitle('Stone Paper Scissors Game')
		setWinIcon(self,C_ROCK)
		setStyleSheet("background-color:cyan;")
		setWindowFlags(Qt_Window | Qt_WindowTitleHint | Qt_WindowCloseButtonHint | Qt_CustomizeWindowHint)
		reSize(900,600)
		winheight 	= height()
		fontSize	= 8 + (winheight / 100)

		for Col = 1 to len(ChoseList)
			Button[Col] = new QPushButton(win) {
				x = 150+(Col-1)*height
				setgeometry(x,35,width,height)
				setStyleSheet("background-color:white;")
				seticon(new qicon(new qpixmap(ChoseList[Col])))
				setIconSize(new qSize(200,200))
				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()

  

You may also check:How to resolve the algorithm Longest string challenge step by step in the Groovy programming language
You may also check:How to resolve the algorithm Mind boggling card trick step by step in the zkl programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Racket programming language
You may also check:How to resolve the algorithm Sequence of primorial primes step by step in the C++ programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the GW-BASIC programming language