|
||||||
| C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Does the following help a bit?
In any case, can anyone help me make it shorter? Can we impart artificial intelligence to player 2? Code:
#include<iostream>
using namespace std;
char board[9] ={'1','2','3','4','5','6','7','8','9'};
void drawboard()
{cout << "\n " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout<< " ---------" << endl;
cout<< board[3] << " | " << board[4] << " | " << board[5] << endl;
cout<< " ---------" << endl;
cout<< board[6] << " | " << board[7] << " | " << board[8] << endl;}
int winning(char f[])
{
if(board[0] == board[1] && board[2] == board[0] )
return 1;
else if(board[3] == board[4] && board[5] == board[3])
return 1;
else if(board[6] == board[7] && board[8] == board[6])
return 1;
else if(board[0] == board[3] && board[6] == board[0])
return 1;
else if(board[1] == board[4] && board[7] == board[1])
return 1;
else if(board[2] == board[5] && board[8] == board[2])
return 1;
else if(board[0] == board[4] && board[8] == board[0])
return 1;
else if(board[2] == board[4] && board[6] == board[2])
return 1;
else
return 0;
}
int main()
{
char c[2] ={'x','o'};
int move;
int player=2;
for(int i=0;i<=9;i++){
if(i<9&&winning(board)==0)
{
drawboard();
if(player ==1) player =2;
else player =1;
cout<<"player"<<player<<" please move"<<endl;
cin>>move;
board[move-1]=c[player-1];
{if(winning(board) == 1)
{cout<<"player"<<player<<" wins."<<endl;
return 0;
}}}
else if(i==9&&winning(board)==0)
cout<<"Tie"<<endl;
else
cin.get();
}
}
|
|
|||||
|
Ok, first off: sorry, I can't help you translate the C# code into C++. I've only used C myself.
Secondly: don't be downhearted that you haven't got minimax to work yet. It is one of those algorithms that's simple to understand, but really tough to implement - especially if you're not that familiar with trees. Your teacher should not have said you couldn't become a good programmer - and he should hever have said you were stupid! The fact that you got the game to work with a different algorithm proves you aren't stupid. If I were you, I would leave minimax and move on to something different. Minimax is a combersome algorithm that has had it's time and isn't really popular in the AI community any more. If you're making a simple game, then minimax is too bulky to implement and you're better off using a simpler algorithm like you did in your game. If you're making a complex game, then the minimax tree tends to grow really huge and you have to bodge on loads of modifications to the algorithm to try and get it running fast enough to play the game. And it's only any good for turn-based games - once you start wanting to do a real-time game, minimax doesn't really apply any more. Also, you said you wanted to make games. Games are complex programs with many different parts. Don't be downhearted that you've got stuck with the AI part. You could always move on to a different aspect of game design for the time being and come back to AI later. A game needs at least these parts in it:
I would suggest reading this introduction to games development. Remember that games take a lot of work and a long time to develop. That's why commercial games studios emply hundreds of people! This site has examples of games that students have made. Lots of them have source code, and you may even be able to contact the people who made them. Well done for getting the grade in your course. Programming is one of those things that is hard to teach, because so much of it is practice. Learning something new in programming usually follows this pattern (in my experience):
The problem is that a lot of programming is very obvious once you've 'got it', but until you have, it seems like the hardest thing ever. But if you keep working at it then you will get it. You won't get it all at once - you will get different bits at different times, and there will always be something that seems like the hardest thing ever, and there will always be some things that you will never get, but that's life. Good luck with your games programming and remember that games programming isn't minimax - there are a million different things out there that you can do that are games programming. And never be afraid to ask questions!
__________________
My fun, friendly online games website: Cygnet Games My Squidoo page on Cygnet Games. |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A simple TicTacToe game | Zunone | C and C++ | 1 | 08-16-2007 12:01 PM |
| Creating a card game (multiplaye online with AI).. what do I program this in? | anothersoldier | General Programming | 1 | 07-19-2007 07:46 AM |
| Paying for Links | Chan | Marketing | 26 | 05-02-2007 09:37 PM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |