Solved

How do I complete this code to work effectively?

Posted on 2008-10-07
4
319 Views
Last Modified: 2012-06-27
                       All in C# language
I seeking help in extending the  implementation of the mimimax algorithm  in  this program so that
(1).  in addition to the variable that holds the minimax value of the node, the node has a

         variable that holds the board position. The value of this variable can simply be an array of

         length nine which takes the values x and o (or 1 and -1, if you prefer integers).

(2).    the tree is generated automatically by computing the successor states for the board

       position represented by a particular node. Start with the empty board for the root node

       and recursively compute the successors until you reach terminal nodes.

(3). the data value of a terminal node is determined by the board position that it represents:

         1, if Max wins

        0, if it is a draw

       -1, if Min wins

(4). after running the program, all nodes should have the appropriate minimax values.
using System;
using System.Collections.Generic;
 
using System.Text;
 
public class Minimax
 
{
 
static void Main(string[] args)
 
{
 
// generate a simple game tree, starting with the root
 
Tree tree = new Tree(0);
 
 
 
// children of the root
 
tree.children = new Forest(new int[] { 0, 0 });
 
// children of the left child of the root
 
tree.children.first.children = new Forest(new int[] { 0, 7, 9 });
 
// children of the right child of the root
 
tree.children.rest.first.children = new Forest(new int[] { 4, 8, 6 });
 
// children of the leftmost child of the left child of the root
 
tree.children.first.children.first.children = new Forest(new int[] { 5, 3, 1 });
 
// write the expected and calculated minimax values to the console
 
Console.WriteLine("Expected minimax value is 5");
 
Console.WriteLine("Calculated minimax value of the tree " + tree.Minimax(true));
 
Console.WriteLine("Press any key to exit ...");
 
Console.ReadKey();
 
} // end method Main
 
private class Tree
 
{
 
public int data; // data stored in node
 
public Forest children; // link to the children of the node
 
// create a tree without children and initialize data
 
public Tree(int nodeData)
 
{
 
data = nodeData;
 
children = null; // node has no children
 
} // end constructor
 
// determine the minimax value of the node
 
public int Minimax(Boolean maximise)
 
{
 
// this method calculates the minimax value for the tree, which is:
 
// - the value in data, if the tree is a leaf
 
if (children == null) return data;
 
// - the maximum of the minimax values of the subtrees, if maximise is true
 
if (maximise) return children.Max();
 
// - the minimum of the minimax values of the subtrees, if maximise is false
 
else return children.Min();
 
} // end method Minimax
 
} // end class Tree
 
private class Forest
 
{
 
public Tree first; // first tree
 
public Forest rest; // remaining trees
 
// create a single-node tree for each piece of data and link them together
 
public Forest(int[] nodeData)
 
{
 
first = new Tree(nodeData[0]);
 
if (nodeData.Length > 1)
 
{
 
rest = new Forest(nodeData, 1);
 
}
 
else rest = null;
 
} // end constructor
 
public Forest(int[] nodeData, int start)
 
{
 
first = new Tree(nodeData[start]);
 
if (nodeData.Length - start > 1)
 
{
 
rest = new Forest(nodeData, start + 1);
 
}
 
else rest = null;
 
} // end constructor
 
// determine the maximum of the minimax values of the trees in the forest
 
public int Max()
 
{
 
// this method returns the maximum of the minimax values of the trees in the forest
 
int max = first.Minimax(false);
 
if (rest != null) max = Math.Max(first.Minimax(false), rest.Max());
 
return max;
 
} // end method Max
 
// determine the minimum of the minimax values of the trees in the forest
 
public int Min()
 
{
 
// this method returns the minimum of the minimax values of the trees in the forest
 
int min = first.Minimax(true);
 
if (rest != null) min = Math.Min(first.Minimax(true), rest.Min());
 
return min;
 
} // end method Min
 
} // end class Forest
 
} // end class Minimax

Open in new window

0
Comment
Question by:Mpencili
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
Busac earned 500 total points
ID: 22676691
So which part you don't know how to do? (And what game are we talking about anyway? :-))
0
 

Author Comment

by:Mpencili
ID: 22676777
This is a tic-tac-toe game.
The required outcome is fully explained in text form accompanying the code.
Cheers.
0
 

Author Closing Comment

by:Mpencili
ID: 31503791
Fake website
0
 
LVL 4

Expert Comment

by:Busac
ID: 22748317
Umm, my previous comment wasn't intended to be a solution to this problem.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question