Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created February 5, 2021 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/8716b6b052e00cc21abcb3746d330736 to your computer and use it in GitHub Desktop.
Save codecademydev/8716b6b052e00cc21abcb3746d330736 to your computer and use it in GitHub Desktop.
Codecademy export
#include <iostream>
#include <vector>
#include <algorithm>
#include "gameplay.h"
bool check_match(int x, int y, int z, const std::vector<int>& acts){
std::vector<int> answer = {x, y, z};
int match_counter = 0;
for (int number : acts) {
for (int i : answer) {
if (number == i) {
match_counter++;
}
}
}
if (match_counter >= 3){
return true;
} else{
return false;
}
}
void show_field(int array[][3]){
std::cout << "===================\n";
for (int i = 0; i < 3; ++i) {
std::cout << "| ";
for (int j = 0; j < 3; ++j) {
if (array[i][j] == 0){
std::cout << "-O-" << " | ";
} else if (array[i][j] == 10){
std::cout << "-X-" << " | ";
} else {
std::cout << " " << array[i][j] << " | ";
}
}
std::cout << std::endl;
}
std::cout << "===================\n";
}
void *add_new_number(int &num, std::vector<int> &entered_numbers) {
std::cout << "\nEnter your number!\n";
std::cin >> num;
while (std::count(entered_numbers.begin(), entered_numbers.end(), num)){
std::cout << "\nYou`ve already have this number! Enter new number!\n";
std::cin >> num;
}
return nullptr;
}
void intro() {
std::cout << "=============================================\n"
"Hello! In this game you have to choose numbers from 1 to 9\n"
"to win you need to make a line of 3 numbers before your opponent\n"
"first player will put the \"-X-\" in the cell\n"
"second player will put the \"-O-\" in the cell\n"
"=============================================\n";
}
bool play_again() {
char sym;
std::cin >> sym;
if (sym == 'N' or sym == 'n') {
std::cout << "Goodbye!\n";
return false;
}
return true;
}
#include <iostream>
#include "gameplay.h"
#include <vector>
int main() {
bool want_to_play = true;
while (want_to_play){
int field[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
intro();
show_field(field);
std::vector<int> player_1_acts;
std::vector<int> player_2_acts;
std::vector<int> current_player_acts;
std::vector<int> entered_number;
int player_switch = 0;
bool we_have_a_winner = false;
int players_chosen_number = 0;
////////////////////////////////////////////////////
//game cycle with question about wanting to continue
while (!we_have_a_winner && entered_number.size() < 9){
add_new_number(players_chosen_number,entered_number);
entered_number.push_back(players_chosen_number);
/////////////////////////////////////////////////////////////
//order of cells in field(array) related by entered number
int string_of_field = (players_chosen_number + 2) / 3 - 1;
int row_of_field = -((9 - players_chosen_number) % 3) + 2;
////////////////////////////////////////////////////////////
if (player_switch == 0){
player_1_acts.push_back(players_chosen_number);
current_player_acts = player_1_acts;
field[string_of_field][row_of_field] = 10;
} else if (player_switch == 1){
player_2_acts.push_back(players_chosen_number);
current_player_acts = player_2_acts;
field[string_of_field][row_of_field] = 0;
}
//
show_field(field);
//
if (check_match(1 ,2, 3, current_player_acts) or
check_match(4 ,5, 6, current_player_acts) or
check_match(7 ,8, 9, current_player_acts) or
check_match(1 ,4, 7, current_player_acts) or
check_match(2 ,5, 8, current_player_acts) or
check_match(3 ,6, 9, current_player_acts) or
check_match(1 ,5, 9, current_player_acts) or
check_match(3 ,5, 7, current_player_acts))
{we_have_a_winner = true;
}
if (we_have_a_winner){
std::cout << "player " << player_switch + 1 << " is winner!\n";
}
///////////////
//player change
player_switch = (player_switch + 1) % 2;
//////////////
}
if (!we_have_a_winner){
std::cout << "\nWe have a dead heat!\n";
}
std::cout << "\nDo you want to play again?\n"
"for YES enter any symbol, for NO enter \"N\"\n";
want_to_play = play_again();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment