473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Tree generation & minimax

I have been trying to learn how to do a c++ tic tac toe game, with first implementing the game tree in a function and then having the AI in another function which uses minimax algorithm. I came across the following code in my search for sources that would help me get a better understanding:

Expand|Select|Wrap|Line Numbers
  1.  
  2. /** evaluate board state from player's point of view */
  3.   int evaluate(Node node, int player) { /* using NEGMAX version of MINIMAX */
  4.      int value = 0;
  5.      if (wonFor(node.state, player)) {
  6.         value = 1; }
  7.      else if (wonFor(node.state, -player)) {
  8.         value = -1; }
  9.      else {
  10.         Vector successors = space.getSuccessors(node, -player);
  11.         Vector evaluations = new Vector();
  12.         for (int i = 0; i < successors.size(); i++) {
  13.            Node successor = (Node)successors.get(i);
  14.            successor.evaluation = evaluate(successor, -player);
  15.            if (successor.evaluation > value) value = successor.evaluation; }
  16.         value = -value; }
  17.      return(value);
  18.   }
  19.  
  20. Tree generation
  21.  
  22.   /** Generate tree of board states with evaluations appended */
  23.   Vector getTree(Node node, int player) {
  24.      Vector tree = new Vector();
  25.      tree.add(evaluate(node, player) + " for " + name(player) + " " + node);
  26.      if (winnerOf(node.state) == 0) {
  27.         Vector children = space.getSuccessors(node, -player);
  28.         for (int i = 0; i < children.size(); i++) {
  29.            tree.add(getTree((Node)children.elementAt(i), -player)); } }
  30.      return(tree);
  31.   }
  32.  
However though it may be close to what I am sort of looking for, I'm not very good at the java language and was wondering if someone could help me in relating it into c++?

-void ttt_gametree_generate(ttt<Item>*, bool): generates a full game tree and the second argument bool indicates if it is the user's turn (true) or the agent's turn (false) // similar to the tree generation maybe?
-int ttt_bestmove(const ttt<Item>*): returns the best move of the agent, maybe what I'm looking for in the firs java function?

I'm trying to learn and having examples would help alot. I'm generating it with linked list over an array just for something different.
Thanks.
May 25 '06 #1
0 6321

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: qwweeeit | last post by:
Hi all, I am developing in Python (as a GUI I choosed Qt). To increase my expertise, besides reading manuals & tutorials, I am studying a big program developed in the language of my choice, and...
1
by: Adalbert | last post by:
First, I'm sorry for my english. Second, I've a little question: is some tree stucture like collections ArrayList or Queue to hold tree of same objects in .NET? Maybe I can use TreeView to that. I...
5
by: Ruthless | last post by:
hello. All XML and XSLT are processed by preprocessor as a trees. How can i simply display my XML as some kind of tree. given xml: <struct> <node level="1" no="1">
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
8
by: marar.harish | last post by:
I need the following programs: Q. Write a c program that will allow you to enter and maintain a computerized version of your family tree.Begin by entering the number of generation.Then enter the...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
3
by: Thorsten Kampe | last post by:
Hi, This is a fairly general question: is there some kind of module or framework that allows building a tree like structure from certain kind of data? To be specific: I have a program that...
1
by: sourab123 | last post by:
hello i got frustrated after making several tries to make the minimax algorithm work in my console tic tac toe. the problem is the algorithm doesn't play the game as it should.The thing is it just...
1
by: j_depp_99 | last post by:
Hi I wrote a program for a two player game that involves the difference between 2 given numbers. The players take turns entering the differences between two numbers that start as random numbers...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.