import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers
public class RpsGame{
public void rps(){
System.out.println("Rock Paper Scissors");
System.out.println("Type r for rock, p for paper, or s for scissors");
Scanner scRPS = new Scanner(System.in);
String userChoice = scRPS.nextLine().toLowerCase();
Boolean quit = false; // Boolean expression example, Unit 3 Topic
int random = (int) (Math.random() * 3);
while(quit == false){ // If statement example, along with boolean expressions is also a Unit 3 Topic
if(userChoice.equals("r")){
if(random == 1){
System.out.println("You chose rock \nThe computer chose paper \nYou lose!");
}
else if(random == 2){
System.out.println("You chose rock \nThe computer chose scissors \nYou win!");
}
else{
System.out.println("You chose rock \nThe computer chose rock \nIt's a tie!");
}
quit = true;
}
else if(userChoice.equals("p")){
if(random == 1){
System.out.println("You chose paper \nThe computer chose paper \nIt's a tie!");
}
else if(random == 2){
System.out.println("You chose paper \nThe computer chose scissors \nYou lose!");
}
else{
System.out.println("You chose paper \nThe computer chose rock \nYou win!");
}
quit = true;
}
else if(userChoice.equals("s")){
if(random == 1){
System.out.println("You chose scissors \nThe computer chose paper \nYou win!");
}
else if(random == 2){
System.out.println("You chose scissors \nThe computer chose scissors \nIt's a tie!");
}
else{
System.out.println("You chose scissors \nThe computer chose rock \nYou lose!");
}
quit = true;
}
else{
System.out.println("Invalid input, try again");
userChoice = scRPS.nextLine();
}
}
scRPS.close();
}
public static void main(String[] args){
RpsGame game1 = new RpsGame(); // creating a new object in Java, Unit 2 Topic
game1.rps();
}
}
// Note, there is an issue with the code not outputting here, but it works perfectly fine on a Java Compiler online
RpsGame.main(null);
Rock Paper Scissors
Type r for rock, p for paper, or s for scissors
You chose rock
The computer chose rock
It's a tie!
import java.util.Scanner;
import java.util.Random;
public class TicTacToe { // creating a class called TicTacToe, which sets us up for creating the actual game, Unit 5 Topic
private Scanner scanner = new Scanner(System.in);
private String[] board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"}; // Array made here, Unit 6 Topic
private String player1 = "X";
private String player2 = "O";
private Random random = new Random();
private int turn = 0;
private boolean quit = false;
public void printBoard() {
for (int i = 0; i < 9; i += 3) { // Iterates through the board array and prints out each element of the board, Unit 4 Topic
System.out.println(board[i] + " | " + board[i + 1] + " | " + board[i + 2]);
}
}
public boolean makeMove(int move) {
if (move < 1 || move > 9 || !board[move - 1].equals(String.valueOf(move))) {
System.out.println("Invalid move. Try again.");
return false;
}
board[move - 1] = player1;
return true;
}
public boolean checkWin() {
int[][] winningCombos = { // 2D Array used here, Unit 8 Topic
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Rows
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Columns
{0, 4, 8}, {2, 4, 6} // Diagonals
};
for (int[] combo : winningCombos) {
if (board[combo[0]].equals(player1) &&
board[combo[1]].equals(player1) &&
board[combo[2]].equals(player1)) {
return true;
}
}
return false;
}
public void playAgainstFriend() {
System.out.println("Type the number of the square you want to place your piece in");
while (!quit) {
System.out.println(player1 + "'s turn");
printBoard();
int move = scanner.nextInt();
if (makeMove(move)) {
turn++;
if (checkWin()) {
System.out.println(player1 + " wins!");
quit = true;
} else if (turn == 9) {
System.out.println("It's a tie!");
quit = true;
} else {
player1 = (player1.equals("X")) ? "O" : "X";
}
}
}
}
public void playAgainstComputer() {
String computer = "O";
System.out.println("Type the number of the square you want to place your piece in");
while (!quit) {
if (player1.equals("X")) {
System.out.println(player1 + "'s turn\n");
printBoard();
int move = scanner.nextInt();
if (makeMove(move)) {
turn++;
if (checkWin()) {
System.out.println(player1 + " wins!");
quit = true;
} else if (turn == 9) {
System.out.println("It's a tie!");
quit = true;
} else {
player1 = "O";
}
}
} else {
System.out.println("Computer's turn\n");
printBoard();
int move2 = random.nextInt(9) + 1;
if (makeMove(move2)) {
turn++;
if (checkWin()) {
System.out.println("Computer wins!");
quit = true;
} else if (turn == 9) {
System.out.println("It's a tie!");
quit = true;
} else {
player1 = "X";
}
}
}
}
}
public void play() {
System.out.println("Tic Tac Toe");
System.out.println("Do you want to play against a friend or the computer?\n");
System.out.println("Type 1 for friend, 2 for computer: ");
int choice = scanner.nextInt();
if (choice == 1) {
playAgainstFriend();
} else if (choice == 2) {
playAgainstComputer();
}
scanner.close();
}
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.play();
}
}
TicTacToe.main(null);
Tic Tac Toe
Do you want to play against a friend or the computer?
Type 1 for friend, 2 for computer:
Type the number of the square you want to place your piece in
X's turn
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Computer's turn
1 | X | 3
4 | 5 | 6
7 | 8 | 9
X's turn
1 | X | 3
4 | 5 | 6
7 | 8 | O
Computer's turn
1 | X | X
4 | 5 | 6
7 | 8 | O
X's turn
1 | X | X
4 | O | 6
7 | 8 | O
Computer's turn
1 | X | X
X | O | 6
7 | 8 | O
X's turn
1 | X | X
X | O | 6
O | 8 | O
Invalid move. Try again.
X's turn
1 | X | X
X | O | 6
O | 8 | O