Created
March 19, 2023 13:57
-
-
Save tasmirz/a12e5bfaae8f2dc300d3c0d83813454c to your computer and use it in GitHub Desktop.
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
#ifndef Bismillah | |
#include <cstdlib> | |
#include <iostream> | |
using namespace std; | |
#define endl '\n' | |
#define range(i, a, b) for (int i = a; i < b; i++) | |
#endif | |
typedef union { | |
struct { | |
unsigned int i9 : 1; | |
unsigned int i8 : 1; | |
unsigned int i7 : 1; | |
unsigned int i6 : 1; | |
unsigned int i5 : 1; | |
unsigned int i4 : 1; | |
unsigned int i3 : 1; | |
unsigned int i2 : 1; | |
unsigned int i1 : 1; | |
} tile; | |
unsigned int value : 9; | |
} board; | |
namespace sudoko { | |
int winingStates[8] = { | |
0b111000000, 0b000111000, 0b000000111, 0b100100100, | |
0b010010010, 0b001001001, 0b100010001, 0b001010100, | |
}; | |
char p1gylph = '*', p2gylph = '#', whitespace = '.'; | |
string welcometext = "Welcome!\n\n"; | |
string movetext = "row column : "; | |
string invalidmove = "Invalid move.\n"; | |
string p1turn = "Player 1's turn\n"; | |
string p2turn = "Player 2's turn\n"; | |
string p1won = "Player 1 won.\n"; | |
string p2won = "Player 2 won.\n"; | |
string tie = "Tie!\n"; | |
} // namespace sudoko | |
bool check(board state) { | |
range(i, 0, sizeof(sudoko::winingStates) / sizeof(*sudoko::winingStates)) { | |
if ((sudoko::winingStates[i] & state.value) == sudoko::winingStates[i]) | |
return true; | |
} | |
return false; | |
} | |
bool checkSpace(board overall, board newmove) { | |
if ((overall.value & newmove.value) == 0) return true; | |
return false; | |
} | |
bool takeMove(board* player, board* overall, board move) { | |
if (checkSpace(*overall, move)) { | |
player->value |= move.value; | |
overall->value |= move.value; | |
return true; | |
} | |
return false; | |
} | |
void drawBoard(board player1, board player2) { | |
range(i, 0, 9) { | |
if ((player1.value >> i) & 1) | |
cout << sudoko::p1gylph; | |
else if ((player2.value >> i) & 1) | |
cout << sudoko::p2gylph; | |
else | |
cout << sudoko::whitespace; | |
if (i % 3 == 2) cout << endl; | |
} | |
} | |
void getMove(board* player, board* overall) { | |
bool invalid = false; | |
board move; | |
do { | |
there: | |
if (invalid) cout << sudoko::invalidmove; | |
cout << sudoko::movetext; | |
unsigned short i, j; | |
cin >> i >> j; | |
if (i < 1 || i > 3 || j < 1 || j > 3) { | |
invalid = true; | |
goto there; | |
} | |
move.value = 1 << ((i - 1) * 3 + j - 1); | |
invalid = true; | |
} while (!takeMove(player, overall, move)); | |
} | |
void loop() { | |
board player1; | |
board player2; | |
board overall; | |
player1.value = 0; | |
player2.value = 0; | |
overall.value = 0; | |
cout << sudoko::welcometext; | |
char c; | |
// cin >> c; | |
int moves = 0; | |
// if (c == 'Y' || 'y') { | |
while (1) { | |
moves++; | |
if (moves > 9) { | |
cout << sudoko::tie; | |
break; | |
} | |
system("clear"); | |
system("clear"); | |
cout << endl << endl; | |
drawBoard(player1, player2); | |
cout << endl << endl; | |
cout << sudoko::p1turn; | |
getMove(&player1, &overall); | |
cout << endl << endl; | |
if (check(player1)) { | |
system("clear"); | |
cout << endl << endl; | |
drawBoard(player1, player2); | |
cout << endl << endl; | |
cout << sudoko::p1won << endl; | |
break; | |
} | |
moves++; | |
if (moves > 9) { | |
cout << sudoko::tie; | |
break; | |
} | |
system("clear"); | |
cout << endl << endl; | |
drawBoard(player1, player2); | |
cout << endl << endl; | |
cout << sudoko::p2turn; | |
getMove(&player2, &overall); | |
if (check(player2)) { | |
system("clear"); | |
cout << endl << endl; | |
drawBoard(player1, player2); | |
cout << endl << endl; | |
cout << sudoko::p2won << endl; | |
break; | |
} | |
cout << endl << endl; | |
} | |
//} | |
} | |
int main() { loop(); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment