Skip to content

Instantly share code, notes, and snippets.

@Enrix835
Created May 21, 2009 16:13
Show Gist options
  • Save Enrix835/115549 to your computer and use it in GitHub Desktop.
Save Enrix835/115549 to your computer and use it in GitHub Desktop.
simple tris game
/*
sTris is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
/* TODO : improve IA */
#if defined(_WIN32) || defined(WIN32) || defined(__WIN32__) || defined(__MSDOS__) || defined(MSDOS)
#define CLRSCR system("cls")
#else
#define CLRSCR system("clear")
#endif
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define EMPTY ' '
enum state {WIN, LOSE, PLAY, DRAW};
enum state status = PLAY;
void info()
{
printf("sTris -- a simple tris game by Enrix835\n");
}
void check_win(int wField[3][3])
{
/* improve with a for */
if (
((wField[0][0] == 'X')&&(wField[0][1] == 'X')&&(wField[0][2] == 'X')) ||
((wField[1][0] == 'X')&&(wField[1][1] == 'X')&&(wField[1][2] == 'X')) ||
((wField[2][0] == 'X')&&(wField[2][1] == 'X')&&(wField[2][2] == 'X')) ||
((wField[0][0] == 'X')&&(wField[1][0] == 'X')&&(wField[2][0] == 'X')) ||
((wField[0][1] == 'X')&&(wField[1][1] == 'X')&&(wField[2][1] == 'X')) ||
((wField[0][2] == 'X')&&(wField[1][2] == 'X')&&(wField[2][2] == 'X')) ||
((wField[0][0] == 'X')&&(wField[1][1] == 'X')&&(wField[2][2] == 'X')) ||
((wField[0][2] == 'X')&&(wField[1][1] == 'X')&&(wField[2][0] == 'X'))
)
{
status = WIN;
}
else if (
((wField[0][0] == 'O')&&(wField[0][1] == 'O')&&(wField[0][2] == 'O')) ||
((wField[1][0] == 'O')&&(wField[1][1] == 'O')&&(wField[1][2] == 'O')) ||
((wField[2][0] == 'O')&&(wField[2][1] == 'O')&&(wField[2][2] == 'O')) ||
((wField[0][0] == 'O')&&(wField[1][0] == 'O')&&(wField[2][0] == 'O')) ||
((wField[0][1] == 'O')&&(wField[1][1] == 'O')&&(wField[2][1] == 'O')) ||
((wField[0][2] == 'O')&&(wField[1][2] == 'O')&&(wField[2][2] == 'O')) ||
((wField[0][0] == 'O')&&(wField[1][1] == 'O')&&(wField[2][2] == 'O')) ||
((wField[0][2] == 'O')&&(wField[1][1] == 'O')&&(wField[2][0] == 'O'))
)
{
status = LOSE;
}
}
bool full(int wField[3][3])
{
register unsigned int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (wField[i][j] == EMPTY) return false;
}
} return true;
}
void print(int wField[3][3])
{
int i , j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("| %c |",wField[i][j]);
}
putchar('\n');
}
}
void clean(int wField[3][3])
{
int i , j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
wField[i][j] = ' ';
}
}
}
void option(int wField[3][3])
{
int i , j;
printf(" 0 1 2\n");
for (i = 0; i < 3; i++) {
printf("%d",i);
for (j = 0; j < 3; j++) {
printf("| %c |",wField[i][j]);
}
putchar('\n');
}
}
void computer(int wField[3][3])
{
int row;
int column;
int card;
printf("\nComputer moves...\n");
for (card = 1; card <= 1; card++) {
do {
row = rand() % 3;
column = rand() % 3;
}while(wField[row][column] != ' ');
wField[row][column] = card;
if (wField[row][column] >= 1) wField[row][column] = 'O';
}
}
void human_1(int wField[3][3])
{
unsigned int x, y;
do {
printf("First player move ... \n?");
scanf("%u %u",&x,&y);
}while(wField[x][y] != ' ');
wField[x][y] = 'X';
}
void human_2(int wField[3][3])
{
unsigned int x, y;
do {
printf("Second player move ... \n?");
scanf("%u %u",&x,&y);
}while(wField[x][y] != ' ');
wField[x][y] = 'O';
}
void user(int wField[3][3])
{
unsigned int x, y;
do {
printf("Human move ... \n?");
scanf("%u %u",&x,&y);
}while(wField[x][y] != ' ');
wField[x][y] = 'X';
}
void human_h(int wField[3][3])
{
CLRSCR;
printf("First player move cross ( X ) and second move circle ( O )\n");
option(wField);
clean(wField);
while (status == PLAY) {
human_1(wField);
print(wField);
check_win(wField);
if (status == WIN) {
printf("Human wins\n");
break;
} else if (status == LOSE){
printf("Computer wins\n");
break;
} else if(full(wField) == true){
printf("Draw\n");
break;
}
human_2(wField);
print(wField);
check_win(wField);
if (status == WIN) {
printf("Human wins\n");
break;
} else if (status == LOSE){
printf("Computer wins\n");
break;
} else if(full(wField) == true){
printf("Draw\n");
break;
}
}
}
void human_c(int wField[3][3])
{
CLRSCR;
option(wField);
clean(wField);
while (status == PLAY) {
user(wField);
print(wField);
check_win(wField);
if (status == WIN) {
printf("Human wins\n");
break;
} else if (status == LOSE){
printf("Computer wins\n");
break;
} else if(full(wField) == true){
printf("Draw\n");
break;
}
computer(wField);
print(wField);
check_win(wField);
if (status == WIN) {
printf("Human wins\n");
break;
} else if (status == LOSE){
printf("Computer wins\n");
break;
} else if(full(wField) == true){
printf("Draw\n");
break;
}
}
}
int main()
{
int field[3][3] = { 0 };
int chc;
srand(time(NULL));
do {
printf("************************\n"
"*** Welcome to sTris ***\n"
"************************\n"
"\n1. Human Vs Human\n"
"2. Human Vs Computer\n"
"3. Information\n\n"
"? ");
scanf("%d",&chc);
switch(chc) {
case 1:
human_h(field);
break;
case 2:
human_c(field);
break;
case 3:
info();
break;
default:
printf("sTris : Wrong selection\n");
}
}while (chc > 3 || chc < 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment