Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

rock paper scissors

My Rock Paper Scissor game:
https://codepen.io/Lorenzo-SC/pen/ExgpPEO
Please give me a like if you appreciate :)
Comment

rock paper scissors javascript

/*
Options
(Note: each option beats the next option in the array
although the last option in the array beats the first.
This means you can add more options if you wish.)
*/
const options = [ "rock", "scissors", "paper" ];

// do while ensures the body is executed at least once
do {
	// get user input
	let input = window.prompt(options.map((option, idx) => "enter " + idx + " for " + option).join(", "));
	// check if the user pressed "Ok" or "Cancel"
	if (typeof input === "string") {
		// remove all non-numeric characters from input
		input = input.replace(/[^0-9]/g, "");
		// if the user has input a valid number
		if (options[input]) {
			// print the user's choice
			window.alert("You picked " + options[input] + "!");
			// generate the computer's choice
			const computer_choice = ~~(Math.random() * options.length);
			// print the computer's choice
			window.alert("The computer picked " + options[computer_choice] + "!");
			// process choices
			let you_beat = input + 1, computer_beats = computer_choice + 1;
			if (you_beat > options.length) {
				you_beat = 0;
			}
			if (computer_beats > options.length) {
				computer_beats = 0;
			}
			// print outcome
			if (you_beat === computer_choice) {
				window.alert("You win! :D");
			} else if (computer_beats === input) {
				window.alert("You loose... :(");
			} else {
				window.alert("It was a tie! :|");
			}
        } else {
			window.alert("Oh no! You didn't enter a number...");
		}
	} else {
		window.alert("Woops! You didn't enter anything or your input was not in the correct format...");
	}
} while (window.confirm("Play again?"));
Comment

rock paper scissors

#include<iostream>
using namespace std;

int main() {

		char options[3] = {'r', 'p', 's'};
    

    cout << "enter your choise from r, p, s or ! to stop play: ";
    char humanChoise;
    cin >> humanChoise;
    int humanPoints = 0, computerPoints = 0;
    while (humanChoise != '!') {
        if (humanChoise != 'p' && humanChoise != 's' && humanChoise != 'r'  && humanChoise != '!') {
            cout << "your choise is wrong try again" << endl;
        } else {
            char computerChoise;
            //for random numbers
            srand (time(NULL));
            int indx = rand() % 3;
            computerChoise = options[indx];
            if (humanChoise == computerChoise) {
                cout << "tie" << endl;
            } else if (humanChoise == 'p') {
                if (computerChoise == 'r') {
                    humanPoints++;
                    cout << "you win" << endl;
                }
                if (computerPoints == 's') {
                    computerPoints++;
                    cout << "you lose" << endl;
                }
            } else if (humanChoise == 'r') {
                if (computerChoise == 's') {
                    humanPoints++;
                    cout << "you win" << endl;
                }
                if (computerPoints == 'p') {
                    computerPoints++;
                    cout << "you lose" << endl;
                }
            } else if (humanChoise == 's') {
                if (computerChoise == 'p') {
                    humanPoints++;
                    cout << "you win" << endl;
                }
                if (computerPoints == 'r') {
                    computerPoints++;
                    cout << "you lose" << endl;
                }
            }
        }
        cout << "Your points: " << humanPoints << endl;
        cout << "Computer points: " << computerPoints << endl;
        cout << "enter your choise from r, p, s or ! to stop play: ";
        cin >> humanChoise;
    }
    
    if (humanPoints >= computerPoints) {
        cout << "You win game!!!" << endl;
    } else if (humanPoints == computerPoints) {
        cout << "tie!!!" << endl;
    } else {
        cout << "You lose game!!!" << endl;
    }

	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript hypot 
Javascript :: do some css using js on selector 
Javascript :: add class with jquery 
Javascript :: Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_0__.storage.ref is not a function 
Javascript :: rails to json 
Javascript :: add element into array 
Javascript :: data-dismiss="modal" in js 
Javascript :: set interval 
Javascript :: js mouse move activate 
Javascript :: router in next js 
Javascript :: .scrollLeft + 1, 0 
Javascript :: how to export mongodb database to json 
Javascript :: lodash swap array elements 
Javascript :: javascript sum of number in object array 
Javascript :: summation js 
Javascript :: find unique value on array 
Javascript :: react native navigation nested 
Javascript :: jquery get tr value 
Javascript :: check if object is not defined js 
Javascript :: groupBy angular 
Javascript :: how to add query parameter to url reactjs 
Javascript :: discord.js edit embed message 
Javascript :: javascript shift everything in array to the right 
Javascript :: how to print every second in javascript 
Javascript :: for of loop 
Javascript :: how to take 100% width in react native 
Javascript :: how to check if the element exist in the parent element javascript 
Javascript :: foreach loop js arrow functons 
Javascript :: mongoose show all indexes 
Javascript :: nuxt 3 plugin 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =