Sure! Here's an implementation of a 3D Tic Tac Toe game in C programming language without using strings and arrays. The game uses simple variables and conditional statements to represent the 3D cube structure.
#include <stdio.h>
// Constants
#define PLAYER1 1
#define PLAYER2 2
#define EMPTY 0
// Variables
int cube[3][3][3]; // 3D Cube
int currentPlayer = PLAYER1;
// Function to initialize the cube with empty values
void initializeCube() {
int i, j, k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
cube[i][j][k] = EMPTY;
}
}
}
}
// Function to print the current state of the cube
void printCube() {
int i, j, k;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 3; k++) {
printf("%d ", cube[i][j][k]);
}
printf("\t");
}
printf("\n");
}
}
// Function to check if a player has won
int checkWin(int player) {
int i, j, k;
// Checking all the rows
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (cube[i][j][0] == player && cube[i][j][1] == player && cube[i][j][2] == player) {
return 1;
}
}
}
// Checking all the columns
for (i = 0; i < 3; i++) {
for (k = 0; k < 3; k++) {
if (cube[i][0][k] == player && cube[i][1][k] == player && cube[i][2][k] == player) {
return 1;
}
}
}
// Checking all the diagonals
for (i = 0; i < 3; i++) {
if (cube[i][0][0] == player && cube[i][1][1] == player && cube[i][2][2] == player) {
return 1;
}
if (cube[i][0][2] == player && cube[i][1][1] == player && cube[i][2][0] == player) {
return 1;
}
}
// Checking diagonals across the faces
if (cube[0][0][0] == player && cube[1][1][1] == player && cube[2][2][2] == player) {
return 1;
}
if (cube[0][0][2] == player && cube[1][1][1] == player && cube[2][2][0] == player) {
return 1;
}
if (cube[0][2][0] == player && cube[1][1][1] == player && cube[2][0][2] == player) {
return 1;
}
if (cube[0][2][2] == player && cube[1][1][1] == player && cube[2][0][0] == player) {
return 1;
}
return 0; // No winner
}
// Function to update the cube with player's move
void makeMove(int face, int row, int column) {
cube[face][row][column] = currentPlayer;
}
// Function to switch between players
void switchPlayer() {
if (currentPlayer == PLAYER1) {
currentPlayer = PLAYER2;
} else {
currentPlayer = PLAYER1;
}
}
int main() {
int face, row, column;
int moves = 0;
initializeCube();
while(1) {
printf("Currently playing: Player %d\n", currentPlayer);
printf("Enter the position [face(0-2), row(0-2), column(0-2)]: ");
scanf("%d%d%d", &face, &row, &column);
if (face >= 0 && face <= 2 && row >= 0 && row <= 2 && column >= 0 && column <= 2) {
if (cube[face][row][column] == EMPTY) {
makeMove(face, row, column);
moves++;
printCube();
if (checkWin(currentPlayer)) {
printf("Player %d wins!\n", currentPlayer);
break;
} else if (moves == 27) { // All the cells are filled and no winner
printf("It's a draw!\n");
break;
} else {
switchPlayer();
}
} else {
printf("Invalid move! Position already taken.\n");
}
} else {
printf("Invalid move! Position out of range.\n");
}
}
return 0;
}
Hope you find this helpful!