Rock Paper Scissor is one of the most common games played by everyone once in his childhood, where two persons use their hands and chooses random objects between rock, paper, or scissor, and their choice decides the winner between them. What if a single person can play this game? With a computer, just by using a single C application, we can design the game Rock Paper Scissor application just using basic C knowledge like if-else statements, random value generation, and input-output of values. Created application has a feature where we can play the game, and maintain the score of Person 1 and Person 2.
The hangman game is one of the most famous games played on computers. The Rules of the game are as follows:
Simple Calculator is a C language-based application used for performing all the simple arithmetic operations like addition, multiplication, division, and subtraction. The application can be made using basic knowledge of C like if-else statements, loops, etc. The functionalities of the application are mentioned below:
The working of tic tac toe game is same as traditional tic tac toe having following components:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to implement the game
int game(char you, char computer)
{
// If both the user and computer
// has choose the same thing
if (you == computer)
return -1;
// If user's choice is stone and
// computer's choice is paper
if (you == 's' && computer == 'p')
return 0;
// If user's choice is paper and
// computer's choice is stone
else if (you == 'p' && computer == 's') return 1;
// If user's choice is stone and
// computer's choice is scissor
if (you == 's' && computer == 'z')
return 1;
// If user's choice is scissor and
// computer's choice is stone
else if (you == 'z' && computer == 's')
return 0;
// If user's choice is paper and
// computer's choice is scissor
if (you == 'p' && computer == 'z')
return 0;
// If user's choice is scissor and
// computer's choice is paper
else if (you == 'z' && computer == 'p')
return 1;
}
// Driver Code
int main()
{
// Stores the random number
int n;
char you, computer, result;
// Chooses the random number
// every time
srand(time(NULL));
// Make the random number less
// than 100, divided it by 100
n = rand() % 100;
// Using simple probability 100 is
// roughly divided among stone,
// paper, and scissor
if (n < 33)
// s is denoting Stone
computer = 's';
else if (n > 33 && n < 66)
// p is denoting Paper
computer = 'p';
// z is denoting Scissor
else
computer = 'z';
printf("\n\n\n\n\t\t\t\tEnter s for STONE, p for PAPER and z for SCISSOR\n\t\t\t\t\t\t\t");
// input from the user
scanf("%c", &you);
// Function Call to play the game
result = game(you, computer);
if (result == -1) {
printf("\n\n\t\t\t\tGame Draw!\n");
}
else if (result == 1) {
printf("\n\n\t\t\t\tWow! You have won the game!\n");
}
else {
printf("\n\n\t\t\t\tOh! You have lost the game!\n");
}
printf("\t\t\t\tYOu choose : %c and Computer choose : %c\n",you, computer);
return 0;
}
#include <stdio.h>
#include <string.h>
#define SIZE_NAME 16
#define SIZE_CHARECTERS 21
/******************
* Function Name:sort
* Input: Array of separated words, number of words
* Output: Void
* Function Operation: Sorting the array
******************/
void sort(char sortedArr[][SIZE_CHARECTERS], int wordNum){
char temp[SIZE_CHARECTERS];
const int ONE = 1;
//sorting the names by alphabetical order, comparing first name
for(int k = 0; k < wordNum - ONE; k++){
//comparing second name
for(int j = k + ONE; j < wordNum; j++){
//replace names only if the left name is bigger then right name
if(strcmp(sortedArr[k], sortedArr[j]) > 0){
//replacing the order
strcpy(temp, sortedArr[k]);
strcpy(sortedArr[k], sortedArr[j]);
strcpy(sortedArr[j], temp);
}
}
}
}
/******************
* Function Name:strList
* Input: Empty array of words, array of words, array for the hint, array of tokens
* Output: Number of elements in the array
* Function Operation: separating array by words
******************/
int strList(char sortedArr[][SIZE_CHARECTERS], char mainStr[], char hint[], char sep[]){
//making a variable to know where the words begin
const char start = ':';
//saving the hint
for(int i = 0; mainStr[i] != start; i++){
hint[i] = mainStr[i];
}
//making a pointer to start from the first word
char *token;
token = strchr(mainStr, start);
//separating the words
token += sizeof(char);
token = strtok(token, sep);
//amount of words in the game
int wordNum = 0;
//coping each word to a new array
while (token != NULL){
strcpy(sortedArr[wordNum], token);
token = strtok(NULL, sep);
wordNum++;
}
//returning the number of words in the game
return wordNum;
}
/******************
* Function Name:shape
* Input:number of stage
* Output:Void
* Function Operation: printing the shape
******************/
void shape(int stage){
//constant integer for the shape stages' lines
const int ZERO = 0, ONE = 1, TWO = 2, THREE = 3, FOUR = 4, FIVE = 5, SIX = 6;
//stage 1 print
if(stage == ONE){
printf(" _________________\n");
for(int t = 0; t < FIVE; t++){
printf("| |\n");
}
printf("|________________|\n");
}
//stage 2 print
else if(stage == TWO){
printf(" _________________\n");
for(int t = 0; t < FIVE; t++){
if(t == ONE || t == TWO){
printf("| ** |\n");
}
else{
printf("| |\n");
}
}
printf("|________________|\n");
}
//stage 3 print
else if(stage == THREE){
printf(" _________________\n");
for(int t = 0; t < FIVE; t++){
if(t == ONE || t == TWO){
printf("| ** ** |\n");
}
else{
printf("| |\n");
}
}
printf("|________________|\n");
}
//stage 4 print
else if(stage == FOUR){
printf(" _________________\n");
for(int t = 0; t < FIVE; t++){
if(t == ZERO){
printf("| -- |\n");
}
else if(t == ONE || t == TWO){
printf("| ** ** |\n");
}
else{
printf("| |\n");
}
}
printf("|________________|\n");
}
//stage 5 print
else if(stage == FIVE){
printf(" _________________\n");
for(int t = 0; t < FIVE; t++){
if(t == ZERO){
printf("| -- -- |\n");
}
else if(t == ONE || t == TWO){
printf("| ** ** |\n");
}
else{
printf("| |\n");
}
}
printf("|________________|\n");
}
//stage 6 print
else if(stage == SIX){
printf(" _________________\n");
for(int t = 0; t < FIVE; t++){
if(t == ZERO){
printf("| -- -- |\n");
}
else if(t == ONE || t == TWO){
printf("| ** ** |\n");
}
else if(t == FOUR){
printf("| \\/\\/\\/\\/\\/\\/\\ |\n");
}
else{
printf("| |\n");
}
}
printf("|________________|\n");
}
}
/******************
* Function Name:game
* Input:sorted array, chosen option to play, length of the option, the hint arrray
* Output:Void
* Function Operation: playing "Hang Man"
******************/
void game(char sortedArr[][SIZE_CHARECTERS], int option, int lenOfOption, char hint[]){
//variable to know in what stage you are
int stage = 1;
//flags for the clue and the same letter statement
int hintFlag = 1;
int outFlag = 1;
int correctFlag = 1;
//array of the answers
char answers[SIZE_CHARECTERS];
//index of the letter in the array
int letIndex = 0;
//choice of the user
char choice = 0;
char enter;
//constant chars for the arrays of the answer and the line
const char UNDERLINE = '_', SPACE = ' ', ASTERISK = '*' ;
//answer line array
char line[lenOfOption];
//making the answer line
for(int i = 0; i < lenOfOption; i++){
//if the spot is a letter
if(sortedArr[option][i] != SPACE){
line[i] = UNDERLINE;
}
//if the spot is a space
else{
line[i] = SPACE;
}
}
//constant integers for the logic tree in the game
const int ZERO = 0, ONE = 1, SIX = 6;
//the game itself!
while(ONE){
//printing the shape
shape(stage);
//checking if the user won, if he did - break
if (strcmp(line, sortedArr[option]) == ZERO){
printf("The word is %s, good job!", sortedArr[option]);
break;
}
//printing the answer line
for(int i = 0; i < lenOfOption; i++){
printf("%c", line[i]);
}
//checking the need of printing the "hint" statement
if(hintFlag == ONE){
printf("\ndo you want a clue? press -> *");
}
//letters the user tried
printf("\nThe letters that you already tried:");
//printing the array of the used letters
for(int k = 0; k < letIndex;k++){
if(k == letIndex - ONE){
printf(" %c", answers[k]);
}
else{
printf(" %c,", answers[k]);
}
}
//asking for a letter
printf("\nplease choose a letter:\n");
scanf("%*[^\n]");
scanf("%*[^\n]%*c");
scanf("%c %c", &enter, &choice);
//if the user wants a clue
if(choice == ASTERISK && hintFlag == ONE){
printf("the clue is: ");
for(int i = 0; i < strlen(hint); i++){
printf("%c", hint[i]);
}
printf(".\n");
hintFlag = 0;
}
//if the user doesn't want a clue
else if(choice != ASTERISK){
//checking if the letter was used
for(int i = 0; i < letIndex; i++){
//if the letter was used
if(choice == answers[i]){
outFlag = 0;
}
}
//if it hasn't been used
if(outFlag == ONE){
//put it in the used letters array
answers[letIndex] = choice;
letIndex++;
//if the letter is correct
for(int i = 0; i < lenOfOption; i++){
if(sortedArr[option][i] == choice){
line[i] = choice;
correctFlag = 0;
}
}
//if the letter is wrong
if(correctFlag == ONE){
//raise a stage
stage++;
//if its the final stage
if(stage == SIX){
shape(stage);
printf("The word is %s, GAME OVER!", sortedArr[option]);
break;
}
}
}
//if the letter was used
else{
printf("You've already tried that letter.\n");
}
}
//redefine all flags
outFlag = 1;
correctFlag = 1;
}
}
int main() {
//the main array of words to be guessed in the game
char mainStr[SIZE_NAME*SIZE_CHARECTERS];
char sortedArr[SIZE_NAME][SIZE_CHARECTERS];
char hint[SIZE_CHARECTERS] = {0};
//asking the user for words
printf("Enter your words:\n");
scanf("%[^\n]", mainStr);
//sending the array of words be separated and getting the number of words
int wordNum = strList(sortedArr, mainStr,hint, ",");
//sending the separated array to be sorted
sort(sortedArr, wordNum);
//printing the sorted array as a list
const int ONE = 1;
printf("choose an option:\n");
for(int i = 0; i < wordNum; i++){
printf("%d: %s\n", (i + ONE), sortedArr[i]);
}
//variable to store the option
int option;
scanf("%d", &option);
option -= ONE;
//checking the length of the word. because the array begin from 0, but the list from 1, so (option- 1).
int lenOfOption = strlen(sortedArr[option]);
//going to start the game!
game(sortedArr, option, lenOfOption, hint);
return 0;
}
// C Program to make a Simple Calculator
// Using switch case
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main()
{
char ch;
double a, b;
while (1) {
printf("Enter an operator (+, -, *, /), "
"if want to exit press x: ");
scanf(" %c", &ch);
// to exit
if (ch == 'x')
exit(0);
printf("Enter two first and second operand: ");
scanf("%lf %lf", &a, &b);
// Using switch case we will differentiate
// operations based on different operator
switch (ch) {
// For Addition
case '+':
printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b);
break;
// For Subtraction
case '-':
printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b);
break;
// For Multiplication
case '*':
printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b);
break;
// For Division
case '/':
printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b);
break;
// If operator doesn't match any case constant
default:
printf(
"Error! please write a valid operator\n");
}
printf("\n");
}
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int won = 0; /* Winning condition */
/* There won't be any double evaluation in a Tic Tac Toe game ;-) */
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) > (b)) ? (b) : (a))
/* For boolean values */
typedef enum { false, true } bool;
/* For player moves */
struct move {
int x;
int y;
}move;
/* Moves are defined as: */
char computer = 'X';
char user = 'O';
void rules () {
/* The basic instructions: */
printf("TIC TAC TOE\n");
printf("MULTIPLAYER GAME\n");
printf("Rules are simple, 3 X's or O's in a row (vertical, horizontal and diagonal)\nEG: X wins belows\n");
printf("X\tO\tO\nX\tX\tX\nO\tO\tX\n");
}
void board_initialization (char board[3][3]) {
/* To initialize every cell as '_' */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = '_';
}
}
}
void check_board (char board[3][3]) {
int i=0, j=0;
char move[2] = {'X','O'};
/* Checking all the 8 win conditions */
for (int cntr = 0; cntr < 2; cntr++) {
if (((board[i][j]==move[cntr]) && (board[i][j+1]==move[cntr]) && (board[i][j+2]==move[cntr])) || ((board[i][j]==move[cntr]) && (board[i+1][j]==move[cntr]) && (board[i+2][j]==move[cntr])) || ((board[i][j]==move[cntr]) && (board[i+1][j+1]==move[cntr]) && (board[i+2][j+2]==move[cntr])) || ((board[i][j+2]==move[cntr]) && (board[i+1][j+1]==move[cntr]) && (board[i+2][j]==move[cntr])) || ((board[i][j+2]==move[cntr]) && (board[i+1][j+2]==move[cntr]) && (board[i+2][j+2]==move[cntr])) || ((board[i][j+1]==move[cntr]) && (board[i+1][j+1]==move[cntr]) && (board[i+2][j+1]==move[cntr])) || ((board[i+1][j]==move[cntr]) && (board[i+1][j+1]==move[cntr]) && (board[i+1][j+2]==move[cntr])) || ((board[i+2][j]==move[cntr]) && (board[i+2][j+1]==move[cntr]) && (board[i+2][j+2]==move[cntr]))) {
printf("\n%c WON\nCONGRATS %c\n", move[cntr], move[cntr]);
won++;
break;
}
}
}
int is_valid (int move_x, int move_y, char board[3][3]) {
/* Out-of-the-grid condition: */
if (move_x > 2 || move_x < 0 || move_y > 2 || move_y < 0)
return false;
/* To prevent illegal move */
if (board [move_x][move_y] == 'O' || board [move_x][move_y] == 'X')
return false;
/* Nothing illegal */
return true;
}
void print_board (char board[3][3]) {
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
printf("%c\t",board[i][j]);
}
printf("\n");
}
}
int move_verification (char board[3][3]) {
/* To check if any more moves can be made */
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (board[i][j] == '_')
return true;
return false;
}
int evaluation (char board[3][3]) {
/* checking ROWS for a victory */
for (int row = 0; row < 3; row++) {
if (board[row][0] == board[row][1] && board[row][1] == board[row][2]) {
if (board[row][0] == computer)
return +10;
else if (board[row][0] == user)
return -10;
}
}
/* checking COLUMNS for a victory */
for (int col = 0; col < 3; col++) {
if (board[0][col] == board[1][col] && board[1][col] == board[2][col]) {
if (board[0][col] == computer)
return +10;
else if (board[0][col] == user)
return -10;
}
}
/* checking DIAGONALS for a victory */
if (board[0][0] == board[1][1] && board[1][1] == board[2][2]) {
if (board[0][0] == computer)
return +10;
else if (board[0][0] == user)
return -10;
}
if (board[0][2] == board[1][1] && board[1][1] == board[2][0]) {
if (board[0][2] == computer)
return +10;
else if (board[0][2] == user)
return -10;
}
/* None of the above conditions are matched */
return 0;
}
int minimax (char board[3][3], int depth, int isMax) {
/* Check for the terminal points ( will return 0 if the depth is yet to be reached ) */
int score = evaluation(board);
/* We will first check whether the game can be terminated with the current possible score */
/* If maximizer has won */
if (score == 10)
return score;
/* If minimizer has won */
if (score == -10)
return score;
/* If no more moves can be made */
if (move_verification(board) == false)
return 0;
/* If it is maximizer's move
* Remember that the goal of maximizer is to attain the highest possible score */
if (isMax == 1) {
/* Initialize a lowest point of comparision */
int best = -1000;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j]=='_') {
board[i][j] = computer; /* Make your move */
/* We are traversing one more step in the tree and are making minimizer play the next move */
best = max(best, minimax(board,depth+1,0)); /* Find the maximizer's move */
board[i][j] = '_'; /* Undo your move */
}
}
}
return best;
}
/* If it is minimizer's move
* Remember that the goal of minimizer is to attain the lowest possible score */
else {
/* Initialize a lowest point of comparision */
int best = 1000;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j]=='_') {
board[i][j] = user; /* Make your move */
/* We are traversing one more step in the tree and are making maximizer play the next move */
best = min(best, minimax(board,depth+1,1)); /* Find the minimizer's move */
board[i][j] = '_'; /* Undo your move */
}
}
}
return best;
}
}
void computer_plays (char board[3][3]) {
/* Initialize the computer moves */
struct move play;
play.x = -1;
play.y = -1;
/* Initialize a lowest point of comparision */
int best_val = -1000;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
/* If a move is valid, try it */
if (board[i][j] == '_') {
board[i][j] = computer; /* Make the move */
int move_val = minimax(board, 0, 0); /* Find the value from the minimax function */
board[i][j] = '_'; /* Undo the move */
/* If the current move is better than the best move, then change the best move */
if (move_val > best_val) {
best_val = move_val;
play.x = i;
play.y = j;
}
}
}
}
/* Finally make the Computer Move */
board[play.x][play.y] = computer;
}
void user_plays (char board[3][3]) {
/* Initialize the user moves */
struct move play;
play.x = -1;
play.y = -1;
/* Accept the moves from user */
printf("\nEnter the x and y co-ordinate of your move rows-(0-2) col-(0-2) : ");
scanf("%d%d", &play.x, &play.y);
/* Check for validity of the move */
if (is_valid(play.x, play.y, board) == true)
board[play.x][play.y] = user;
else {
printf("Not A Valid Move!\n");
user_plays(board);
}
}
void computer_move (char board[3][3]) {
/* A collection of computer's calls */
printf("\nComputer is playing it's move ...\n");
computer_plays(board);
check_board(board);
print_board(board);
}
void user_move (char board[3][3]) {
/* A collection of user's calls */
user_plays(board);
check_board(board);
print_board(board);
}
int toss (void) {
printf("Time for the toss: \n");
/* A random number between 0 and 1 is accepted as the toss and returned back.
* Heads or Tails is decided by the computer. */
srand(time(NULL));
int toss = rand() % 2;
if (toss == 0)
printf("User wins the toss ! Play first MATE !\n");
else if (toss == 1)
printf("Computer wins the toss ! Let me begin by kicking your ass !\n");
return toss;
}
void gameplay (char board[3][3], int toss) {
/* Win condition should be 1 or moves should not be left to terminate the loop */
while (won != 1 && move_verification(board) == true) {
/* Checks for user's chance to play */
if (toss == 0) {
user_move(board);
if (won == 1)
break;
toss = 1;
continue;
}
/* Checks whether any move is left on board */
else if (move_verification(board) == false) {
printf("It's a draw !\n");
break;
}
/* Checks for computer's chance to play */
else if (toss == 1) {
computer_move(board);
if (won == 1)
break;
toss = 0;
}
}
/* checks whether computer played the last move */
if (toss==0)
printf("It's a draw !\n");
}
int main (void) {
char board[3][3]; /* Tic Tac Toe board */
rules();
board_initialization(board);
print_board(board);
gameplay(board, toss());
return 0;
}