tic-tac-toe in c – dead fun, innit
intro
Oi, you lot! New to C and fancy summat a bit cheeky? We’re gonna sling together a lil program to play Tic-Tac-Toe—ya know, that 3x3 grid where ya chuck X’s and O’s til someone wins or it’s a draw. It’s dead simple but proper cracking—like when I was tryna beat my mate at it on a pub table (drew, too smashed to focus). Great for getting yer head round arrays and loops in C without a right faff. Let’s have a butcher’s!
code bit
Here’s the code—nowt posh, just a quick job I chucked together for two players. Shoved it in a box so it don’t look like a total shambles on yer blog. take a gander:
char board[3][3]; // 3x3 grid, innit
void initBoard() { // blank it out
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
board[i][j]=' '; // empty spaces
}
}
}
void showBoard() { // chuck it on screen
printf("\n");
for(int i=0;i<3;i++) {
printf(" %c | %c | %c \n",board[i][0],board[i][1],board[i][2]);
if(i<2) printf("---+---+---\n");
}
printf("\n");
}
int checkWin(char player) { // see if someone’s won
for(int i=0;i<3;i++) { // rows and cols
if(board[i][0]==player&&board[i][1]==player&&board[i][2]==player) return 1;
if(board[0][i]==player&&board[1][i]==player&&board[2][i]==player) return 1;
}
if(board[0][0]==player&&board[1][1]==player&&board[2][2]==player) return 1; // diag 1
if(board[0][2]==player&&board[1][1]==player&&board[2][0]==player) return 1; // diag 2
return 0;
}
int isFull() { // grid full?
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(board[i][j]==' ') return 0; // still space
}
}
return 1; // packed out
}
int main() { // nearly did void, ha!
initBoard(); // start fresh
char player='X'; // X goes first
int row,col;
while(1) { // keep playing
showBoard(); // show it
printf("Player %c, gimme row (0-2): ",player);
scanf("%d",&row);
printf("Player %c, gimme col (0-2): ",player);
scanf("%d",&col);
if(row<0||row>2||col<0||col>2||board[row][col]!=' ') {
printf("oi, pick a proper empty spot!\n");
continue;
}
board[row][col]=player; // chuck it in
if(checkWin(player)) {
showBoard();
printf("Player %c wins! Nice one!\n",player);
break;
}
if(isFull()) {
showBoard();
printf("It’s a draw, mate!\n");
break;
}
player=(player=='X')?'O':'X'; // swap players
}
return 0; // forgot ; once, ugh!
}
what ya get
Here’s what it chucks out when ya run it. I played a quick game—worked bang on, mate! (Sample run—yer moves’ll differ!)
| |
---+---+---
| |
---+---+---
| |
Player X, gimme row (0-2): 1
Player X, gimme col (0-2): 1
| |
---+---+---
| X |
---+---+---
| |
Player O, gimme row (0-2): 0
Player O, gimme col (0-2): 0
[keeps going...]
O | |
---+---+---
| X |
---+---+---
| | X
Player X wins! Nice one!
how it works, kinda
Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of pork scratchings, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a doddle once ya clock it, swear down!
1. that `#include
This is me nicking my toolkit. `
2. `char board[3][3];` bit
`board[3][3]` is my lil grid—3x3 array of chars, starts empty with spaces. Like a pub table ready for X’s and O’s!
3. `initBoard` funtion
`initBoard()` blanks it out—nested `for` loops set each `board[i][j]=' '`—like wiping the table clean!
4. `showBoard` funtion
`showBoard()` chucks it on screen—loops through rows, prints ` %c | %c | %c ` with bars, adds `---+---+---` between ‘em. Nice and tidy, like a chalkboard!
5. `checkWin` funtion
`checkWin(char player)` sniffs for a win—checks rows, cols, and diags with `if(board[i][0]==player&&...`. Returns 1 if three match, 0 if not—like spotting a winning line!
6. `isFull` funtion
`isFull()` checks if it’s packed—loops through, if any `board[i][j]==' '`, returns 0 (space left). Else, 1—grid’s stuffed!
7. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `initBoard()` starts it, `player='X'` kicks off, then a `while(1)` loop keeps it rolling—shows board, grabs `row` and `col`, checks ‘em, swaps `player` with `(player=='X')?'O':'X'`. `int` means I’m lobbing a 0 out at the end—nearly wrote `void main()`, old me was a right numpty!
8. game loop and checks
Inside `while(1)`, `showBoard()` shows it, `scanf` nabs `row` and `col`. If `row` or `col` ain’t 0-2 or spot’s taken, yells “oi, proper spot!” and loops back. Else, `board[row][col]=player` chucks it in. `checkWin(player)` or `isFull()` breaks it with a win or draw—prints and scarpers!
9. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.
And that’s yer lot! A cracking lil program to play Tic-Tac-Toe. Faff with it—chuck in daft moves, tweak it to say “X smashes it” if ya fancy. First time I ran this, I won with X’s diagonal—bang on, eh! You’re a star already, mate—keep smashing it! 😊