rockpaperscissors
Player inputs strings "rock" "paper" or "scissors" and the botchoice is taken at random. Result will display & PlayAgain button will reset the scene.
C# script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class Manager : MonoBehaviour
{
public Text Result;
public Image AIChoice;
public Image PChoice;
public string input;
public string[] Choices;
public Sprite Rock, Paper, Scissors, q_img;
public UnityEngine.UI.InputField UserInput;
public void ReadStringInput()
{
string input = UserInput.text;
Debug.Log(input);
Play(input);
void Play(string myChoice)
{
string randomChoice = Choices[Random.Range(0, Choices.Length)];
switch(randomChoice)
{
case "Rock":
AIChoice.sprite = Rock;
switch(myChoice)
{
case "Rock":
Result.text = "Tie!";
PChoice.sprite = Rock;
break;
case "Paper":
Result.text = "The paper covers the rock, you win!";
PChoice.sprite = Paper;
break;
case "Scissors":
Result.text = "The rock destroys the scissors, you lose!";
PChoice.sprite = Scissors;
break;
default:
Result.text = "Invalid Input";
PChoice.sprite = q_img;
AIChoice.sprite = q_img;
break;
}
break;
case "Paper":
AIChoice.sprite = Paper;
switch (myChoice)
{
case "Rock":
Result.text = "The paper covers the rock, you lose!";
PChoice.sprite = Rock;
break;
case "Paper":
Result.text = "Tie!";
PChoice.sprite = Paper;
break;
case "Scissors":
Result.text = "The scissors cuts the paper, you win!";
PChoice.sprite = Scissors;
break;
default:
Result.text = "Invalid Input";
PChoice.sprite = q_img;
AIChoice.sprite = q_img;
break;
}
break;
case "Scissors":
AIChoice.sprite = Scissors;
switch (myChoice)
{
case "Rock":
Result.text = "The rock destroys the scissors, you win!";
PChoice.sprite = Rock;
break;
case "Paper":
Result.text = "The scissors cuts the paper, you win!";
PChoice.sprite = Paper;
break;
case "Scissors":
Result.text = "Tie!";
PChoice.sprite = Scissors;
break;
default:
Result.text = "Invalid Input";
PChoice.sprite = q_img;
AIChoice.sprite = q_img;
break;
}
//Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
break;
}
}
}
public void resetscene()
{
Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name);
}
public void Exitscene()
{
Result.text = "Ok bye!";
Application.Quit();
}
}
// Choices = ['rock','paper','Scissors']
// Choices.Length = 3
//Random.Range(0, Choices.Length) = 0/1/2
//Choices[1] = 'paper'
Status | Released |
Platforms | HTML5 |
Author | sushmita.narayana |
Made with | Unity |
Leave a comment
Log in with itch.io to leave a comment.