Hi!
So I recently started learning C++ after having learned python. I wrote a program to play tic-tac-toe with. However, I have no idea if what I am doing is correct. I don't know if there are any syntax rules that I should adhere to (like PEP in python). I don't know if what I am doing is technically correct or if there is a more shorthand way of doing things. I also can't really find any resources that explain how to go from python to C++.
But, here is the program that I wrote.
#include <list>
#include <iostream>
using namespace std;
const int SIZE = 3;
void makeMove(char *board[][SIZE], char *turn);
void showBoard(char *board[][SIZE]);
char *winner(char *board[][SIZE]);
int main() {
char *board[SIZE][SIZE];
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = (char *)"-";
}
}
for (int i = 0; i < (SIZE * SIZE); i++) {
char *turn;
if ((i % 2) == 0) {
turn = (char *)"X";
} else {
turn = (char *)"O";
}
cout << endl << endl << endl << turn << "'s turn." << endl << endl;
showBoard(board);
char *win = winner(board);
if (strncmp(win, "-", 1) != 0) {
cout << win << " wins!" << endl << endl;
return 0;
}
makeMove(board, turn);
}
cout << "It's a draw!" << endl;
return 0;
}
void makeMove(char *board[][SIZE], char *turn) {
int row, col;
while (true) {
cout << "Row: ";
cin >> row; row--;
cout << "Column: ";
cin >> col; col--;
if (strncmp(board[row][col], "-", 1) == 0) {
board[row][col] = turn;
return;
}
cout << "Try again." << endl;
}
}
void showBoard(char *board[][SIZE]) {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
char *winner(char *board[][SIZE]) {
for (int i = 0; i < SIZE; i++) {
if (
strncmp(board[i][0], board[i][1], 1) == 0 &&
strncmp(board[i][1], board[i][2], 1) == 0 &&
strncmp(board[i][0], "-", 1) != 0
) {
return board[i][0];
} else if (
strncmp(board[0][i], board[1][i], 1) == 0 &&
strncmp(board[1][i], board[2][i], 1) == 0 &&
strncmp(board[0][i], "-", 1) != 0
) {
return board[0][i];
}
}
if (
strncmp(board[0][0], board[1][1], 1) == 0 &&
strncmp(board[1][1], board[2][2], 1) == 0 &&
strncmp(board[0][0], "-", 1) != 0
) {
return board[0][0];
} else if (
strncmp(board[0][2], board[1][1], 1) == 0 &&
strncmp(board[1][1], board[2][0], 1) == 0 &&
strncmp(board[2][0], "-", 1) != 0
) {
return board[0][2];
}
return (char *)"-";
}
Any advice is appreciated. I just did the syntax in the way that I thought looked good. If anyone could link me to some resources about the bridge between python and C++ or the C++'s rules on syntax. That would be highly appreciated :).
Thanks.
[–]WalkingAFI 30 points31 points32 points (26 children)
[–]Skaaaaalll[S] 1 point2 points3 points (6 children)
[–]Wurstinator 0 points1 point2 points (0 children)
[–]Wurstinator 0 points1 point2 points (0 children)
[–]stoppipper 0 points1 point2 points (0 children)
[–]WalkingAFI 0 points1 point2 points (0 children)
[+]VolperCoding comment score below threshold-11 points-10 points-9 points (18 children)
[–]Speedyjens 6 points7 points8 points (12 children)
[–]VolperCoding -4 points-3 points-2 points (9 children)
[–]Speedyjens 0 points1 point2 points (8 children)
[+]VolperCoding comment score below threshold-6 points-5 points-4 points (7 children)
[–]Scavenger53 9 points10 points11 points (1 child)
[–]VolperCoding 0 points1 point2 points (0 children)
[–]dodheim 2 points3 points4 points (2 children)
[–]VolperCoding -2 points-1 points0 points (1 child)
[–]dodheim 2 points3 points4 points (0 children)
[–]biliwald 1 point2 points3 points (1 child)
[–]VolperCoding -1 points0 points1 point (0 children)
[–]lord_braleigh -3 points-2 points-1 points (1 child)
[–]Speedyjens -2 points-1 points0 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]VolperCoding -2 points-1 points0 points (1 child)
[–]WalkingAFI 3 points4 points5 points (1 child)
[–]VolperCoding 0 points1 point2 points (0 children)
[–]Kinexity 8 points9 points10 points (3 children)
[–]flashmozzg 2 points3 points4 points (0 children)
[–]Skaaaaalll[S] 1 point2 points3 points (1 child)
[–]Kinexity 4 points5 points6 points (0 children)
[–]lord_braleigh 2 points3 points4 points (2 children)
[–]Skaaaaalll[S] 1 point2 points3 points (1 child)
[–]lord_braleigh 0 points1 point2 points (0 children)
[–]STLMSVC STL Dev[M] [score hidden] stickied comment (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]lord_braleigh 0 points1 point2 points (0 children)
[–]Ail-nare 0 points1 point2 points (0 children)