There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: JAVA Theory


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
21-May-2008, 09:06 PM #16
Does it matter if the original array isn't private?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
22-May-2008, 02:29 AM #17
No, it doesn't matter.
If you mean to work directly on the array of the starter class from a method of the AI class, you'll simply make the called class dependent on a specific attribute of the caller.
Therefore, it will make no sense to use a second class any more. You may as well implement the analyze method into the starter class.
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
22-May-2008, 08:17 PM #18
You've given me good answers....and now I've got to do some thinking of my own and sort out what approach will work best for my situation, I guess I'm still trying to make the transition from procedural to Object-Oriented. So, then, I'll see where this leads, and see what new problems and questions pop-out of that, which I'm sure there will be.
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
23-May-2008, 06:32 PM #19
How would I go about passing something to another class, and then return other stuff (more then one variable) to the original class..?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
23-May-2008, 08:23 PM #20
The methods of a Java class may accept many passed arguments but they only returns either no value or only one value.
But most of the time, we need methods that return more than one value.
In that situation, we have 2 possibilities :
- the expected values have all the same data type and the same role or function
- the expected values have either different data types or their role are different

For the first possibility, the solution is simple :
if you know the exact number of the values, you have to make the method return an array;
if the number of values is not known, then you have to use the List interface and you make the method return a List.

For the second possibility, you'll have to build an object that will contain the values you wish and make the method return that object.
To do so, you'll have to write a new class.
For example, in your Tic Tac Toe program, you want your AI class a method that gives the x and y of the cell to play.
You may write a class called Position that will contain the values.
Example :
Code:

public class Position {
   private int x;
   private int y;
   public Position(int x, int y) {
      this.x = x;
      this.y = Y;
   }
   public int getX() {
      return x;
   }
   public int getY() {
      return y;
   }
}

public class AI {
   ...
   public Position analyze(int[][] board) {
      ...
      int x = something;
      int y = something;
      return new Position(x, y);
   }

public class Starter {
   ...
   ...
   int[][] b = { {0, 0, 0}, {0, 0, 0}, {0, 0, 0} };
   AI ai = new AI();
   Position position = ai.analyze(b);
   int x = position.getX();
   int y = position.getY();
   ...
   ...
 }
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
24-May-2008, 04:26 PM #21
I see....this is going to turn out really interesting, and should be really rewarding when its all finished....this is definitely the route i need to be taking, so, ill see where i can go from there. Just a couple of Clarifications on your code for understanding....I've read about the "this" statement, but i still don't clearly understand its function.
2) The Method Position Analyze....That's references the Position Object (just the Method name, I know it does in the code - basicly why is it "Position Analyze", but when referenced in the starter program its just plain "Analyze")?
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
24-May-2008, 05:53 PM #22
1) this is the reference of the current class instance.
In the Position class of my example, this.x and this.y refer to the members of the class :
private int x;
private int y;


Also , in the statements :
this.x = x;
this.y = y;

x and y are the arguments passed to the constructor.
It is a classic way to intialize the members of a class via its constructor.

2) public Position analyze(int[][] board) : the word Position in the method declaration means that the method must absolutely return a reference of an instance of the Position class.
It's the reason why you see the statement return new Position(x, y); at the end of the method.

A method that returns no value has the word void in its declaration.
In the Position class, the getX() and getY() methods must return a value of the data type int.
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
24-May-2008, 07:23 PM #23
Okay, I think that actually makes since.
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
24-May-2008, 08:56 PM #24
One other snippet of code that's a little confusing: Position position = ai.analyze(b);. I understand the front half is declaring a new object based off the position class, but i dont understand how the last half works when its not new Object();. Again, my textbook for Java provides no insight here...
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
25-May-2008, 06:36 AM #25
If you look at the prior statement AI ai = new AI(); , I create a new instance of the AI class and the reference of the instance is assigned to ai.
If you type System.out.println(ai);, the console will display the name of the class, AI, followed by hexadecimal values which represent the pointer of the object in memory.
Once I have the reference of a class, I may access to the public methods of the class, therefore, the notation ai.analyze(b) makes sense.

Also, it is not mandatory to use variables to access the methods of a class, you may use anonymous instance of a class.
Instead of having :
AI ai = new AI();
Position position = ai.analyze(b);

you may do :
Position position = new AI().analyze(b);
In both cases, position will contain the reference of the Position object returned by the analyze method of the AI instance.
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
25-May-2008, 12:22 PM #26
I'm doing something wrong here and I don't know what....

Code:
import java.util.Random;
public class AI2 {
    Random g = new Random();
    private int x,y, x1, y1;
    private boolean placement = false;
    public Position analyze(int[][] Board, int round) {        
        if (round > 3) {
            for (int i = 0; i < 3; i++) {
                if (Board[i][0] == 1 && Board[i][1] == 1) {
                    x = i;
                    y = 2;
                    placement = true;
                    break;       
                } else if (Board[0][i] == 1 && Board[1][i] == 1) {
                    x = 2;
                    y = i;
                    placement = true;
                    break;
                }
            }
            if (placement = false) {
                if (Board[0][0] == 1 && Board[1][1] == 1) {
                    x = 2;
                    y = 2;
                } else if (Board[0][2] == 1 && Board[1][1] == 1) {
                    x = 2;
                    y = 0;
                }
            }
        } else {
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (Board[i][j] == 1) {
                        x1 = i;
                        y1 = j;
                        placement = true;
                        break;
                    }
                }
                if (placement == true) break;
            }
            do {
             if (x1 == 0) x = g.nextInt(2); else if (x1 == 1) x = g.nextInt(3); else if (x1 == 2) x = g.nextInt(2) + 1;
             if (y1 == 0) y = g.nextInt(2); else if (y1 == 1) y = g.nextInt(3); else if (y1 == 2) y = g.nextInt(2) + 1;
            } while (x == x1 && y == y1);
        }
        return new Position(x, y);
    } 
}
And then the Position class is exactly as you discribed it, no changes there. When I call Position.getX(); and Position.getY(); it always returns as 0,0.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
25-May-2008, 06:01 PM #27
At a first glance, it looks incomplete. Assuming that a cell may contain 3 values : one for each player and a default one when the cell is empty, you only focus the tests for one player and you don't check if a cell has already been used by the second player (the computer I guess).
I've tested your class with the number of rounds <=3, I get random numbers.
The following part :
Code:

            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if (Board[i][j] == 1) {
                        x1 = i;
                        y1 = j;
                        placement = true;
                        break;
                    }
                }
                if (placement == true) break;
            }
            do {
             if (x1 == 0) x = g.nextInt(2); else if (x1 == 1) x = g.nextInt(3); else if (x1 == 2) x = g.nextInt(2) + 1;
             if (y1 == 0) y = g.nextInt(2); else if (y1 == 1) y = g.nextInt(3); else if (y1 == 2) y = g.nextInt(2) + 1;
            } while (x == x1 && y == y1);
may be simplified this way :
Code:

   int v;
   x = 0;
   y = 0;
   while (Board[x][y] == 1) {
      v = g.nextInt(9);
      x = v / 3;
      y = v % 3;
   }
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
26-May-2008, 02:26 PM #28
One last small problem, should be quick to solve, don't revise my code, just tell me in general what i need to do or what I'm doing wrong. From my tests this:
Code:
if (placement = false) {
                if (Board[0][0] == 1 && Board[1][1] == 1) {
                    x = 2;
                    y = 2;
                } else if (Board[0][2] == 1 && Board[1][1] == 1) {
                    x = 2;
                    y = 0;
                }
            }
appears to be getting ignored.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,598 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
26-May-2008, 04:56 PM #29
Without knowing the content of Board, I can't tell you what is wrong.

BTW, for the Tic Tac Toe algorithm :
- the number of rounds to consider may vary according to the player who starts first the game : if the computer starts the game itself, threats are coming from the fourth round otherwise threats are coming from the third;
- when the critical number of rounds is reached, the analyze method should determine first if the computer can win the game, if so, the method returns immediately the position of the empty cell that will ends the game;
- if the computer can't win, then the method should determine where is the enemy's threat and return immediately the position of the empty cell to block;
- if there's no threat, the method returns the position of a random remaining empty cell : some preferences may be given to the strategic cells like the center cell, the corners cells.
Geek4Life's Avatar
Senior Member with 107 posts.
 
Join Date: Jan 2008
Experience: Intermediate
26-May-2008, 07:08 PM #30
It's meant to be a sheerly Defensive AI, for now, I plan on building a fuller AI later.
Reply


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

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 Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:11 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.