-
-
Save 98chimp/9ba6e32fa18180ee6e48 to your computer and use it in GitHub Desktop.
Guessing Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// Guessing Game | |
// | |
// Created by Shahin on 2015-03-10. | |
// Copyright (c) 2015 98% Chimp. All rights reserved. | |
// | |
#include <stdio.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
bool isRunning = true; | |
typedef struct { | |
int lives; | |
int points; | |
char name; | |
} Player; | |
Player resetPlayer (int lives, int points, char name); | |
Player player1; | |
Player player2; | |
Player currentPlayer; | |
int input, answer, question1, question2; | |
int turn = 0; | |
bool correct; | |
void setNames() { | |
printf("Welcome to our Math Game!\n"); | |
printf("Please enter your name\n"); | |
scanf("%s", &player1.name); | |
player1.points = 0; | |
player1.lives = 3; | |
printf("Please enter opponent's name\n"); | |
scanf("%s", &player2.name); | |
player2.points = 0; | |
player2.lives = 3; | |
} | |
void question() { | |
question1 = arc4random() % 21; | |
question2 = arc4random() % 21; | |
if (turn % 2 == 0) { | |
printf("%c's turn: what is %d + %d?\n", player1.name, question1, question2); | |
} | |
else { | |
printf("%c's turn: what is %d + %d?\n", player2.name, question1, question2); | |
} | |
scanf("%d", &answer); | |
if (question1 + question2 == answer) { | |
printf("correct!\n"); | |
correct = true; | |
if (turn % 2 == 0) { | |
player1.points ++; | |
} | |
else { | |
player2.points ++; | |
} | |
printf("%c's score: %d points; %d lives\n", player1.name, player1.points, player1.lives); | |
printf("and %c's score: %d points; %d lives\n", player2.name, player2.points, player1.lives); | |
} | |
else { | |
printf("wrong!\n"); | |
correct = false; | |
if (turn % 2 == 0) { | |
player1.lives --; | |
} | |
else { | |
player2.lives --; | |
} | |
printf("%c's score: %d points; %d lives\n", player1.name, player1.points, player1.lives); | |
printf("and %c's score: %d points; %d lives\n", player2.name, player2.points, player2.lives); | |
} | |
turn++; | |
if (input == 2) { | |
exit(0); | |
} | |
} | |
int mainMenu(int input){ | |
printf("Welcome to our Math Game!\n"); | |
printf("Press 1 to start the game\n"); | |
printf("Press 2 to exit\n"); | |
scanf("%d", &input); | |
if (input == 2) { | |
return isRunning = false; | |
} | |
else if (input == 1) { | |
return isRunning = true; | |
} | |
return 0; | |
} | |
int main() { | |
#pragma mark isRunning | |
while (isRunning) { | |
mainMenu(input); | |
setNames(); | |
while (player1.lives > 0 && player2.lives > 0) { | |
question(); | |
} | |
if (player1.lives == 0) { | |
printf("%c has won the game!\n", player2.name); | |
} | |
else { | |
printf("%c has won the game!\n", player1.name); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment