R-S-P Requirement:
- write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins).
- User's input along with computer's random pick will be encoded in the following format:
-- user's rock: 10
-- user's scissor: 20
-- user's paper: 30
-- comp's rock: 1
-- comp's scissor: 2
-- comp's paper: 3
Then add user's input and computer's pick toghter to for a summation, and use switch statement over the summation to tell the result. For example, 12 (10 + 2: u's rock and c's scissor, user wins), 33 (30 + 3: u's paper and c's paper, draw).
T-T-T Requirement:
- write one C++ program that uses two dimensional array to allow two players to play tic-tac-toe. The program asks for moves alternately from player X and player O to enter a number (1 - 9) they wish to mark to play the game.The program displays the game positions as follows:
1 2 3
4 5 6
7 8 9
After each move, the program displays the changed board, for example:
X X O
4 5 6
O 8 9
- the program should have following functions:
-- a function to take in user's input.
-- a function to print out the board and move.
-- a function to tell if game is over.
-- a function to tell who wins (or draw.)
Note: These have to be combined in a single program.
RPS Program:
/* C++ program that simulates Rock, Paper & Scissors Game */
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
//Getting numeric value from char input
int getNumericVal(char ch)
{
int ip;
//Assigning numeric input
switch(ch)
{
case 'R':
case 'r': ip = 10; break;
case 'P':
case 'p': ip = 30; break;
case 's':
case 'S': ip = 20; break;
default:
ip = -1; cout << "\n Invalid input \n"; break;
}
return ip;
}
//User selection
char Selection()
{
char userIP;
int ip;
//Loop till user enters valid input
do
{
//Reading input from user
cout << "\n\n Please enter your move \n ( 'R' - Rock \t 'P' -
Paper \t 'S' - Scissors) : ";
cin >> userIP;
ip = getNumericVal(userIP);
}while(ip < 10 || ip > 30);
//Returning input
return userIP;
}
//Function that plays a random move
int Computer()
{
//For handling computer move
int computerMove;
//Generating a random move
computerMove = rand() % 3 + 1;
//Assigning numeric input
switch(computerMove)
{
case 1: cout << "\n Computer's move is Rock \n"; break;
case 2: cout << "\n Computer's move is Scissors \n"; break;
case 3: cout << "\n Computer's move is Paper \n";
break;
}
return computerMove;
}
//Function that finds the winner of round
void Winner(char playSel, int compSel)
{
int player1, total;
//Getting numeric values
player1 = getNumericVal(playSel);
//Calculating total
total = player1 + compSel;
//Checking total
switch(total)
{
case 11: cout << "\n Draw \n"; break;
case 12: cout << "\n User Wins \n"; break;
case 13: cout << "\n Computer Wins \n"; break;
case 21: cout << "\n Computer Wins \n"; break;
case 22: cout << "\n Draw \n"; break;
case 23: cout << "\n User Wins \n"; break;
case 31: cout << "\n User \n"; break;
case 32: cout << "\n Computer Wins \n"; break;
case 33: cout << "\n Draw \n"; break;
}
}
//Main program
int main()
{
char playerSelection;
int computerSelection;
srand (time(NULL));
//Reading user selection
playerSelection = Selection();
//Reading computer selection
computerSelection = Computer();
//Playing a round
Winner(playerSelection, computerSelection);
return 0;
}
Sample Run:
_______________________________________________________________________________________________
TTT Game:
#include <iostream>
#include <time.h>
#include <random>
using namespace std;
// Initializing board
void initialize(char board [3][3])
{
int i, j, k=1;
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
board[i][j] =
char(k + 48);
k++;
}
}
}
// print prints the board in nice format
void print_board (char board [3][3])
{
int i, j;
cout << "\n\n The board is: \n";
cout << "\n |---|---|---| \n";
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
//Printing character
cout << " | " << board[i][j];
}
cout << " |";
//Printing footer
cout << "\n |---|---|---| \n";
}
}
//Function that checks whether board is full or not
int isBoardFull(char board[3][3])
{
int i, j;
//Outer loop for rows
for(i=0; i<3; i++)
{
//Inner loop for columns
for(j=0; j<3; j++)
{
//Board is not Full
if(board[i][j]
!= 'X' || board[i][j] != 'O')
return 0;
}
}
//Board Full
return 1;
}
// win returns true if the given player has won on the
// given board, else it returns false
int win (char board [3][3], char player)
{
return (board[0][0] == player && board[0][1] == player
&& board[0][2] == player) ||
(board[1][0] == player &&
board[1][1] == player && board[1][2] == player) ||
(board[2][0] == player &&
board[2][1] == player && board[2][2] == player) ||
(board[0][0] == player &&
board[1][0] == player && board[2][0] == player) ||
(board[0][1] == player &&
board[1][1] == player && board[2][1] == player) ||
(board[0][2] == player &&
board[1][2] == player && board[2][2] == player) ||
(board[0][0] == player &&
board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player &&
board[1][1] == player && board[2][0] == player);
}
//Function that checks for winner
int check_board(char board[3][3], char player1, char player2)
{
//Checking for winning of player
if(win(board, 'X') == 1 && win(board, 'O') == 1)
{
return 2;
}
//Checking for winning of player
else if(win(board, 'X') == 1)
{
return 0;
}
//Checking for winning of computer
else if(win(board, 'O') == 1)
{
return 1;
}
//Tie
else
{
return -1;
}
}
//Function that plays the game
void playGame(char board[3][3], char player1, char player2)
{
int row, col;
int winner;
//Loop till board is full
while(isBoardFull(board) != 1)
{
//Player turn
while(1)
{
//Reading positions from user
cout << "\n Enter the row, column pair for inserting "
<< player1 << " : ";
cin >> row >> col;
//Making suitable for array indexing
row = row - 1;
col = col - 1;
//Checking for empty position
if(board[row][col] != 'X' || board[row][col] != 'O')
break;
else
cout << "\n Invalid choice... Try again!!
\n ";
}
//Storing in array
board[row][col] = player1;
//Printing board
print_board(board);
//Finding winner
winner = check_board(board, player1, player2);
//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: cout << "\n Player 1 won the game... \n";
break;
case 1: cout << "\n Player 2 won the game... \n";
break;
case 2: cout << "\n Game Tie ... \n"; break;
}
return;
}
//Player turn
while(1)
{
//Reading positions from user
cout << "\n Enter the row, column pair for inserting "
<< player2 << " : ";
cin >> row >> col;
//Making suitable for array indexing
row = row - 1;
col = col - 1;
//Checking for empty position
if(board[row][col] != 'X' || board[row][col] != 'O')
break;
else
cout << "\n Invalid choice... Try again!!
\n ";
}
//Storing in array
board[row][col] = player2;
//Printing board
print_board(board);
//Finding winner
winner = check_board(board, player1, player2);
//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: cout << "\n Player 1 won the game... \n";
break;
case 1: cout << "\n Player 2 won the game... \n";
break;
case 2: cout << "\n Game Tie ... \n"; break;
}
return;
}
}
}
//Main function
int main()
{
//Board
char board[3][3];
//Player characters
char player1Character='X', player2Character='O';
//Initializing random function
srand(time(NULL));
cout << "\n\n\t\t TIC TAC TOE \n\n";
cout << "\n\t\t WELCOME \n\n";
cout << "\n Instructions: The object of Tic Tac Toe is to get
three in a row. \n\n";
//Initializing board
initialize(board);
//Printing board
print_board(board);
//Playing game
playGame(board, player1Character, player2Character);
return 0;
}
Sample Run:
Get Answers For Free
Most questions answered within 1 hours.