From 0ba6fe5be7ba298c946618c10a2049f6c5327d96 Mon Sep 17 00:00:00 2001 From: Leon Vatthauer Date: Wed, 27 Nov 2024 17:10:19 +0100 Subject: [PATCH] init and work on implementation --- .vscode/settings.json | 5 ++ a.out | Bin 0 -> 19064 bytes ttt.cc | 139 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 .vscode/settings.json create mode 100755 a.out create mode 100644 ttt.cc diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9d08b58 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..ef198a59dd15ff4efac80e9460d38fb9ba75bdf2 GIT binary patch literal 19064 zcmeHP4RBo5b-ufjEg5WUZEPZbf)|6q#%QgTWZ5PG){>w61d_egTHrWwo>#joX<@ay z>^{kogTX9JMHDlaWY={{QRjFb7 zo%^$TTJJWopzUNvcjmow&$;KEd+xdSzIWfHbz){frpdCow1=h zj!N2EHjjDO5_S!n4N5UC4=*7oatfZXivk5pC0`LB$(2)vL(dfG;}~NKDhmmc++^~| ztbpD!fhx!x0w=jyl$AJhcnAaT;oyhTFa;G~wmv6kW6UY|6e^L6Rz-ppk#MxTx1x7#ZAER3D;aZD3BA;wN?g?K8#lEw`yMtzG*z#S*{0xjiGzhF z;-$6`Kgm;~uDgWi7+U~%C9ZN@)HK!4i;<^v8LrE5sbRMedF3l)*`>G^;hINCPLe}} zQ&nFmyNO6Pe2$kgHebTI64JCJ+poY?B588~$zDpNn`|U~LbtGL?fNlp2XuN z_K#mG`=e_=J~wC46LahP8t;7$9@jn8^`RJz#=y|1p zJ8#hewe(bb_DsjCcgyA@y7>`^A%^hZ#mkS1f8I zO@G2P3}$TenUQ3p&vd&3o&JPjCj4PD*%;8Y7SO8O{K;^@h$YQL$lui%*kWkD)kXkP zo9^_PYYZdM+v|3_s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ureq@>kz9)p+n_SmA@+-fJhA5 zadYu1g?daX=E5SD!b9a!`cnbgzobt|d`|OJUgVKb;64GecjC3C>ww@>Br6}6^6LK= zJXU7p@%b<%)dI81t9>Z-KMc+(+_Z->HR(X{shF)_?W-QgcRHmX3BcpnD68sMdNzQD zV=*z+ezgy~Pcp16ATP;m6Ed_1tK`)_?TD18czR0IM)IAiU+tIP1sa*Eh5Q%fn&GX8y7aWUo1aeUJ- +#include + +using namespace std; + + +const int ROWS = 3; +const int COLUMNS = 3; + +/* Grid representing the tic-tac-toe field. + 0 is empty, + 1 is human, + -1 is computer. +*/ +int grid[COLUMNS][ROWS]; + +void printGrid() { + for (int y = 0; y < ROWS; y++) { + for (int x = 0; x < 2 * COLUMNS + 1; x++) { + cout << '-'; + } + cout << endl; + for (int x = 0; x < COLUMNS; x++) { + if (x == 0) { + cout << '|'; + } + if (grid[x][y] > 0) { + cout << 'X'; + } else if (grid [x][y] < 0) { + cout << 'O'; + } else { + cout << ' '; + } + cout << '|'; + } + cout << endl; + if (y == COLUMNS - 1) { + for (int x = 0; x < 2 * COLUMNS + 1; x++) { + cout << '-'; + } + cout << endl; + } + } +} + + +bool parse_move(string move, int &x, int &y) { + // split string on comma + string x_str; + string y_str; + int pos = move.find(','); + + // error if no comma found + if (pos == string::npos) { + return false; + } + + // split on ',' + x_str = move.substr(0, pos); + y_str = move.substr(pos + 1, move.length() - 1 - pos); + + // convert to int + int x_local = stoi(x_str); + int y_local = stoi(y_str); + + // check conversion and return expected result + if (x_local != -1 && y_local != -1) { + x = x_local; + y = y_local; + return true; + } + + return false; +} + +void player_move() { + string move; + int x; + int y; + while (true) { + cout << "Enter a valid move. (Format is 'x,y')" << endl; + cin >> move; + if (!parse_move(move, x, y)) { + cout << "Couldn't parse move." << endl; + continue; + } + if (x < 0 || y < 0 || x >= COLUMNS || y >= ROWS) { + cout << "Coordinates out of bounds." << endl; + continue; + } + break; + } + grid[x][y] = '1'; +} + +void computer_move() { +} + +bool game_over() { + return false; +} + +/* Play one game of tic tac toe, +/* returns 0 if the player won and 1 if the computer won. +*/ +int play_game() { + // Query for starting player + int startPlayer = -1; + while (startPlayer != 0 && startPlayer != 1) { + cout << "Which player should start? (0 - You, 1 - Program)"; + cin >> startPlayer; + } + + // Do move according to startPlayer + if (startPlayer == 1) { + computer_move(); + } + + while(true) { + player_move(); + if (game_over()) break; + computer_move(); + if (game_over()) break; + } + + + return 0; +} + +int main(int argc, char *argv[]) { + cout << "Hello world" << endl; + for (int y = 0; y < ROWS; y++) { + for (int x = 0; x < COLUMNS; x++) { + grid[x][y] = 1; + } + } + printGrid(); + player_move(); +} \ No newline at end of file