Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #11 (permalink)  
Old 08-13-2007, 04:26 AM
saurav saurav is offline
Newbie
 
Join Date: Aug 2007
Posts: 8
Rep Power: 0
saurav is on a distinguished road
Default

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();
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 08-14-2007, 11:27 AM
CygnetGames's Avatar   
CygnetGames CygnetGames is offline
Programmer
 
Join Date: May 2007
Location: York, England
Posts: 113
Rep Power: 6
CygnetGames is on a distinguished road
Default

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:
  • A physics engine to control how objects can move about in the world.
  • A rendering engine to control how objects are displayed on screen.
  • A control engine to handle starting new games, pausing, saving/loading, etc.
  • An AI engine to control the computer's behaviour.
  • Some graphics to make the game look good.
Obviously, different games have these things to different levels of complexity. And you can choose which of them you want to work on at any time.

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):
  • Find out about a cool new programming thing.
  • Wonder how to implement it.
  • Try to code it, and fail.
  • Bang head against wall.
  • Try to code it a different way, fail.
  • Bang head against wall.
  • Read about it, look at other people's code.
  • Go back to your old code, look at it again.
  • Throw away your old code and start again from scratch, fail.
  • Get a little further than before, but still fail.
  • Bang head against wall.
  • Do something else.
  • Suddenly have a revelation about what you were doing wrong.
  • Go back to your code and fix some things - get further with it.
  • Get stuck at a different problem.
  • Bang head against wall.
  • ... Repeat the above steps a few times ...
  • Finally manage to bodge together something that works. - Yipee!
  • Do something else
  • Come back a long time later to your old program - look at it - think "why the heck did I do it like that??!?
  • Re-write your old program in a day
  • Think - "that was actually quite simple - why was I having trouble with it?"
Anyone else agree with this pattern of programming?
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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


All times are GMT -5. The time now is 02:20 AM.

Contest Stats

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

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads