Text Based Car Racing Game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TRACK_LENGTH 20
void print_track(int player_position, int opponent_position);
int get_move();
int main() {
srand(time(NULL));
int player_position = 0;
int opponent_position = 0;
printf("Welcome to the Text-based Car Racing Game!\n");
printf("Your car: [P]\nOpponent car: [O]\n");
while (1) {
print_track(player_position, opponent_position);
int move = get_move();
player_position += move;
opponent_position += rand() % 3 + 1; // opponent moves randomly
if (player_position >= TRACK_LENGTH) {
printf("You win!\n");
break;
}
else if (opponent_position >= TRACK_LENGTH) {
printf("You lose!\n");
break;
}
}
return 0;
}
void print_track(int player_position, int opponent_position) {
printf("\n");
for (int i = 0; i < TRACK_LENGTH; i++) {
if (i == player_position) {
printf("[P]");
}
else if (i == opponent_position) {
printf("[O]");
}
else {
printf("[ ]");
}
}
printf("\n");
}
int get_move() {
int move;
while (1) {
printf("Enter a move (1-3): ");
scanf("%d", &move);
if (move >= 1 && move <= 3) {
break;
}
printf("Invalid move. Please enter a move between 1 and 3.\n");
}
return move;
}
Cli Based Rock Paper Scissor Game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROCK 1
#define PAPER 2
#define SCISSORS 3
int main() {
int playerScore = 0;
int computerScore = 0;
int rounds = 0;
printf("Welcome to Rock-Paper-Scissors game!\n\n");
while(1) {
int playerMove;
printf("Enter your move (1 = Rock, 2 = Paper, 3 = Scissors, 0 = Quit): ");
scanf("%d", &playerMove);
if (playerMove == 0) {
break;
}
if (playerMove < 1 || playerMove > 3) {
printf("Invalid move! Please try again.\n");
continue;
}
int computerMove = rand() % 3 + 1;
printf("You played: ");
switch(playerMove) {
case ROCK:
printf("Rock\n");
break;
case PAPER:
printf("Paper\n");
break;
case SCISSORS:
printf("Scissors\n");
break;
}
printf("Computer played: ");
switch(computerMove) {
case ROCK:
printf("Rock\n");
break;
case PAPER:
printf("Paper\n");
break;
case SCISSORS:
printf("Scissors\n");
break;
}
if (playerMove == computerMove) {
printf("It's a tie!\n");
} else if (playerMove == ROCK && computerMove == SCISSORS ||
playerMove == PAPER && computerMove == ROCK ||
playerMove == SCISSORS && computerMove == PAPER) {
printf("You win this round!\n");
playerScore++;
} else {
printf("Computer wins this round!\n");
computerScore++;
}
rounds++;
printf("Current score: You %d - %d Computer\n\n", playerScore, computerScore);
}
printf("\nFinal score: You %d - %d Computer\n", playerScore, computerScore);
printf("Total rounds played: %d\n", rounds);
return 0;
}
Cli Based Rock Paper Scissor Game