Skip to content

Instantly share code, notes, and snippets.

@Zhomart
Created March 6, 2014 23:04
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 Zhomart/9401647 to your computer and use it in GitHub Desktop.
Save Zhomart/9401647 to your computer and use it in GitHub Desktop.
assignment 2
#include <iostream>
#include <cstdio>
#include <fstream>
using namespace std;
#define INVALID "i"
#define WIN_X "x"
#define WIN_O "o"
#define TIE "t"
#define CONTINUES "c"
bool have_won(string s, char c){
if (s[0] == c && s[1] == c && s[2] == c) return true;
if (s[3] == c && s[4] == c && s[5] == c) return true;
if (s[6] == c && s[7] == c && s[8] == c) return true;
if (s[0] == c && s[3] == c && s[6] == c) return true;
if (s[1] == c && s[4] == c && s[7] == c) return true;
if (s[2] == c && s[5] == c && s[8] == c) return true;
if (s[0] == c && s[4] == c && s[8] == c) return true;
if (s[2] == c && s[4] == c && s[6] == c) return true;
return false;
}
string proceed(string s){
if (s.length() != 9) return INVALID;
for (int i=0;i<9;++i)
if (s[i] != 'x' && s[i] != 'o' && s[i] != '#')
return INVALID;
// checks for number of x-es and o-s
// it is impossible that abs(x - o) > 1
int x_count = 0;
int o_count = 0;
int sharp_count = 0;
for (int i=0;i<9;++i){
if (s[i] == 'x') x_count++;
if (s[i] == 'o') o_count++;
if (s[i] == '#') sharp_count++;
}
if (abs(x_count - o_count) > 1) return INVALID;
// checks for winners
bool x_win = have_won(s, 'x');
bool o_win = have_won(s, 'o');
if (x_win && o_win) return INVALID;
if (x_win) return WIN_X;
if (o_win) return WIN_O;
if (sharp_count == 0) return TIE;
return CONTINUES;
}
int main(){
freopen("tttstatus.txt", "w", stdout);
ifstream in_file("tictactoeboards.txt");
if (in_file.good())
{
string s;
while (getline(in_file, s)){
cout<<proceed(s)<<endl;
}
return 0;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment