Hey guys, me again. Sorry to pester you so much, but once again I am stuck on an assignment. I'm supposed to create a game of tic tac toe that engages the user with a simple AI from the computer. Now, I have the whole thing mapped out except for one part...checking the win condition. Here's what code I have so far:
However, I need some help writing the checkWin function which checks the entire board for any win conditions. I'm completely stumped, and I have no idea how to go about this. Although code is appreaceated, even just some prompts on how I might go about this task would be greatly appreciated. Also, I was wondering if there was anyway to make the computer use that strategy where you make a triangle and block your opponent off in an attempt to stalemate the game. I've gotten the ai to the point where it's semi-smart, with the following code:Code:// TicTacToeVsAI.c.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "simpio.h" #include <stdlib.h> #define size 3 #define maxMoves 9 void initBoard(char board[size][size]); //Initializes the board for the first time void clearBoard(char board[size][size]); //Initializes the board with 'empty' spaces void printBoard(char board[size][size]); //Shows the current status of the board bool checkWin(char board[size][size]); //Checks if anyone has won at the current time bool TicTacToe(char board[size][size]); //Initializes the game void GetMove(char board[size][size]); //Reads in and places the player's choice of location void CompMove(char board[size][size]); //Causes the computer to make a move main() { char board[size][size]; printf("To play the simple game of tic tac toe, enter the number of the space which \n"); printf("you wish to place your mark in. When either player manages to get 3 in a row, \n"); printf("diagonally, vertically, or horizontally, that player wins, otherwise,\nthe game is a draw.\n"); initBoard(board); printBoard(board); clearBoard(board); TicTacToe(board); } void initBoard(char board[size][size]) { int row,column; int num=49; //Since this is going to initialize the board in such a way as to show //the user how to play, we use num to label the squares of the board for (row=0;row<size;row++) { for (column=0;column<size;column++) { board[row][column]=num; num++; } } } void clearBoard(char board[size][size]) { int row,column; for (row=0;row<size;row++) { for (column=0;column<size;column++) { board[row][column]=' '; //By filling the board with spaces, we are 'clearing' the board. } } } void printBoard(char board[size][size]) { int row,column; for (row=0;row<size;row++) { for (column=0;column<size;column++) { printf("%c", board[row][column]); if (column!=(size-1)) printf("|"); } if (row!=(size-1)) printf("\n-+-+-\n"); } printf("\n"); } bool TicTacToe(char board[size][size]) { int player=1; int move=0; //The number of moves that have been made so far this game. while (move<=maxMoves) { if (player==1) GetMove(board); else CompMove(board); printBoard(board); player*=-1; } return true; } void GetMove(char board[size][size]) { int choice; bool flag=true; printf("Player, please enter your choice of location: "); while (flag) { choice=GetInteger(); switch (choice) { case 1: if (board[0][0]==' ') {board[0][0]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 2: if (board[0][1]==' ') {board[0][1]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 3: if (board[0][2]==' ') {board[0][2]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 4: if (board[1][0]==' ') {board[1][0]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 5: if (board[1][1]==' ') {board[1][1]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 6: if (board[1][2]==' ') {board[1][2]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 7: if (board[2][0]==' ') {board[2][0]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 8: if (board[2][1]==' ') {board[2][1]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; case 9: if (board[2][2]==' ') {board[2][2]='X'; flag=false;} else printf("Invalid choice, try again\n"); break; } } }
But it still gets killed by anyone with half a brain. Again, thanks in advance for any helpCode:void NoLose(char board[size][size],int row,int column) { if (board[row][column]==' ') board[row][column]='O'; else { if ((row==2)&&(column==0)) NoLose(board,row-2,column+1); if ((row==0)&&(column==1)) NoLose(board,row+2,column+1); if ((row==0)&&(column==0)) NoLose(board,row+1,column+2); if ((row==0)&&(column==2)) NoLose(board,row+1,column-2); if ((row==1)&&(column==0)) NoLose(board,row+1,column+2); if ((row==1)&&(column==2)) NoLose(board,row+1,column-2); if ((row==2)&&(column==1)) NoLose(board,row-2,column+1); if ((row==2)&&(column==2)) NoLose(board,row-1,column-2); } }



LinkBack URL
About LinkBacks


