|
He'll spear your brains if you step out of line.
|
You're being too vague. NOBODY can code what you want until you figure out EXACTLY what it is that you want.
Write pseudocode! A tip, your array should only contain values 0 to 3. 4 and 5 should be used for the AI only. Here's what you do: Write pseudocode, this will help you find out EXACTLY what you want. Start simple, just getting the very basics down. Next, start coding. This will be mostly switching out what you just did with actual code, but you may run into a few bumps. Once you get a working algorithm, you can refine it. A big part will be your definition of "best outcome." ![]() ![]() ![]() ![]() ![]() ![]() [quote=Felagund]The most dangerous weapon anyone can wield is self righteousness.[/quote]
I have a signature yay! |
|
Sorry for the huge lines of code next:
Code/** * Write a description of class GUI here. * * @author (your name) * @version (a version number or a date) */ import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; import java.awt.Color; public class GUI { private final static int WSIZEW = 428; private final static int WSIZEH = 628; public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("Blob Wars"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(WSIZEW, WSIZEH); IPanel panel = new IPanel(WSIZEW, WSIZEH); panel.setBackground(Color.white); frame.setResizable(false); frame.setContentPane(panel); frame.show(true); frame.setVisible(true); } } class IPanel extends JPanel implements MouseListener, MouseMotionListener { private final static int BSIZE = 400; private final static int GridResolution = 8; private final static int GRID = BSIZE / GridResolution; private static int menu = 0; //Stage of menu. 0 = Main Menu, 1 = Board Selection (1p game OR 2p game), 2 = Instructions, 3 = in game private static int board = 1; //Which board to display in Board Selection / which Board to use in game private static int player; //Number of players (1p game or 2p game) private static int turn = 1; //Players turn (player 1's turn or player 2's turn) private static int gameOver; //0 = No Winners, 1 = P1 Wins, 2 = P2 wins private static int selectX; //x co-ordinate of blob selected (used for jumping) private static int selectY; //y co-ordinate of blob selected (used for jumping) private Board b; //Decleration of Board object named b private Font f = new Font("Arial",Font.BOLD,30); IPanel(int Window_SizeW, int Window_SizeH){ addMouseListener(this); } // end IPanel() constructor public void paintComponent(Graphics g){ super.paintComponent(g); drawGame(g); } // end paintComponent // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // private void drawGame(Graphics g){ if(menu == 0){ drawMenu(g); } else if(menu == 1){ drawBoardOptions(g); } else if(menu == 2){ drawInstructions(g); } else{ int x=10; int y=10; g.setColor(Color.black); for(x = 10; x <= BSIZE + 10; x += GRID){ g.drawLine(x, y, x, y + BSIZE); } x=10; for (y = 10; y <= BSIZE + 10; y += GRID){ g.drawLine(x, y, x + BSIZE, y); } drawOptions(g); drawBoard(g); drawWinLose(g); if(turn == 2 && player == 1){ b.AI(); repaint(); } } }// end drawGrid() private void drawMenu(Graphics g){ Toolkit Kit = Toolkit.getDefaultToolkit(); Image p1_Image = Kit.getImage("players1.png"); Image p2_Image = Kit.getImage("players2.png"); Image instruction_Image = Kit.getImage("instructions.png"); g.drawImage(p1_Image, 114, 50, this); g.drawImage(p2_Image, 114, 200, this); g.drawImage(instruction_Image, 65, 350, this); }// end drawMenu() private void drawBoard(Graphics g){ Toolkit Kit = Toolkit.getDefaultToolkit(); Image null_Image = Kit.getImage("nullSpace.png"); Image adj_Image = Kit.getImage("adjacentSpace.png"); Image jump_Image = Kit.getImage("jumpSpace.png"); Image red_Image = Kit.getImage("redBlob.png"); Image blue_Image = Kit.getImage("blueBlob.png"); int tempArray[][] = b.getArray(); for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(tempArray[i][j] == 1){ g.drawImage(null_Image, (i*50)+11, (j*50)+11, this); } else if(tempArray[i][j] == 2){ g.drawImage(red_Image, (i*50)+11, (j*50)+11, this); } else if(tempArray[i][j] == 3){ g.drawImage(blue_Image, (i*50)+11, (j*50)+11, this); } else if(tempArray[i][j] == 4){ g.drawImage(jump_Image, (i*50)+11, (j*50)+11, this); } else if(tempArray[i][j] == 5){ g.drawImage(adj_Image, (i*50)+11, (j*50)+11, this); } } } repaint(); }// end drawBoard() private void drawBoardOptions(Graphics g){ Toolkit Kit = Toolkit.getDefaultToolkit(); Image b1_Image = Kit.getImage("b1.png"); Image b2_Image = Kit.getImage("b2.png"); Image b3_Image = Kit.getImage("b3.png"); Image b4_Image = Kit.getImage("b4.png"); Image ok_Image = Kit.getImage("ok.png"); Image board1_Image = Kit.getImage("board1.png"); Image board2_Image = Kit.getImage("board2.png"); Image board3_Image = Kit.getImage("board3.png"); Image board4_Image = Kit.getImage("board4.png"); Image main_Image = Kit.getImage("MainMenu.png"); g.drawImage(b1_Image, 39, 50, this); g.drawImage(b2_Image, 139, 50, this); g.drawImage(b3_Image, 239, 50, this); g.drawImage(b4_Image, 339, 50, this); g.drawImage(ok_Image, 164, 400, this); g.drawImage(main_Image, 65, 475, this); if(board == 1){ g.drawImage(board1_Image, 114, 150, this); } else if(board == 2){ g.drawImage(board2_Image, 114, 150, this); } else if(board == 3){ g.drawImage(board3_Image, 114, 150, this); } else{ g.drawImage(board4_Image, 114, 150, this); } }// end drawBoardOptions() private void drawInstructions(Graphics g){ Toolkit Kit = Toolkit.getDefaultToolkit(); Image instructs_Image = Kit.getImage("instructs.png"); Image main_Image = Kit.getImage("MainMenu.png"); g.drawImage(instructs_Image, 65, 150, this); g.drawImage(main_Image, 65, 475, this); }// end drawInstructions() private void drawOptions(Graphics g){ Toolkit Kit = Toolkit.getDefaultToolkit(); Image pass_Image = Kit.getImage("Pass.png"); Image quit_Image = Kit.getImage("Quit.png"); g.drawImage(pass_Image, 40, 475, this); g.drawImage(quit_Image, 240, 475, this); } private void drawWinLose(Graphics g){ g.setFont(f); if(gameOver == 0 && turn == 1){ g.setColor(Color.red); g.drawString("Player 1's Turn",100,450); } else if(gameOver == 0 && turn == 2){ g.setColor(Color.blue); g.drawString("Player 2's Turn",100,450); } else if(gameOver == 1){ g.setColor(Color.red); g.drawString("Player 1 Wins!",100,450); } else{ g.setColor(Color.blue); g.drawString("Player 2 Wins!",100,450); } } // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // // public void mousePressed(MouseEvent evt) { if(menu == 3){ int mX = evt.getX()/50; int mY = evt.getY()/50; if(mX > 7){mX = 7;} if(mY > 7){mY = 7;} int tempArray[][] = b.getArray(); if(tempArray[mX][mY] == 2 && turn == 1){ b.clearArray(); b.checkArray(mX, mY); selectX = mX; selectY = mY; } if(tempArray[mX][mY] == 3 && turn == 2 && player == 2){ b.clearArray(); b.checkArray(mX, mY); selectX = mX; selectY = mY; } if(tempArray[mX][mY] == 5 && turn == 1){ b.setArray(mX, mY, 2, selectX, selectY, 1); b.clearArray(); turn = 2; } if(tempArray[mX][mY] == 5 && turn == 2 && player == 2){ b.setArray(mX, mY, 3, selectX, selectY, 1); b.clearArray(); turn = 1; } if(tempArray[mX][mY] == 4 && turn == 1){ b.setArray(mX, mY, 2, selectX, selectY, 2); b.clearArray(); turn = 2; } if(tempArray[mX][mY] == 4 && turn == 2 && player == 2){ b.setArray(mX, mY, 3, selectX, selectY, 2); b.clearArray(); turn = 1; } gameOver = b.checkWin(); } }//end mousePressed public void mouseClicked(MouseEvent evt){ int mX = evt.getX(); int mY = evt.getY(); if(mX > 114 && mX < 314 && mY > 50 && mY < 150 && menu == 0){menu = 1; player = 1;}//1 Player Game Button else if(mX > 114 && mX < 314 && mY > 200 && mY < 300 && menu == 0){menu = 1; player = 2;}//2 Player Game Button else if(mX > 114 && mX < 314 && mY > 350 && mY < 450 && menu == 0){menu = 2;}//Instructions Button else if(mX > 39 && mX < 89 && mY > 50 && mY < 100 && menu == 1){board = 1;}//Board 1 Button else if(mX > 139 && mX < 189 && mY > 50 && mY < 100 && menu == 1){board = 2;}//Board 2 Button else if(mX > 239 && mX < 289 && mY > 50 && mY < 100 && menu == 1){board = 3;}//Board 3 Button else if(mX > 339 && mX < 389 && mY > 50 && mY < 100 && menu == 1){board = 4;}//Board 4 Button else if(mX > 65 && mX < 365 && mY > 500 && mY < 600 && menu != 3){menu = 0; board = 1;}//Main Menu Button else if(mX > 164 && mX < 264 && mY > 400 && mY < 450 && menu == 1){menu = 3; b = new Board(board);}//OK Button else if(mX > 40 && mX < 190 && mY > 475 && mY < 575 && menu == 3){if(turn==1){turn = 2;}else{turn = 1;};}//Pass Button else if(mX > 240 && mX < 390 && mY > 475 && mY < 575 && menu == 3){menu = 0; board = 1; turn = 1;}//Quit Button repaint(); } //end mouseClicked public void mouseReleased(MouseEvent evt) { } //Not-needed mouse event public void mouseDragged(MouseEvent evt) {} //Not-needed mouse event public void mouseMoved(MouseEvent evt) { } //Not-needed mouse event public void mouseEntered(MouseEvent evt) { } //Not-needed mouse event public void mouseExited(MouseEvent evt) { } //Not-needed mouse event } |
|
Codepublic class Board { public int x=3; //X position of best possible move (AI) public int y=4; //Y position of best possible move (AI) public int curMost; //Current best option. public int boardArray[][] = new int[8][8]; public Board(int boardType){ if(boardType == 1){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ boardArray[i][j] = 0; } } //Players boardArray[0][6] = 2; boardArray[7][0] = 2; boardArray[0][7] = 3; boardArray[7][7] = 3; } if(boardType == 2){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ boardArray[i][j] = 0; } } //Players boardArray[0][0] = 2; boardArray[7][0] = 2; boardArray[0][7] = 3; boardArray[7][7] = 3; //Null Tiles boardArray[2][2] = 1; boardArray[2][5] = 1; boardArray[3][3] = 1; boardArray[3][4] = 1; boardArray[4][3] = 1; boardArray[4][4] = 1; boardArray[5][2] = 1; boardArray[5][5] = 1; } if(boardType == 3){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ boardArray[i][j] = 0; } } //Players boardArray[0][0] = 2; boardArray[7][0] = 2; boardArray[0][7] = 3; boardArray[7][7] = 3; //Null Tiles boardArray[0][1] = 1; boardArray[0][6] = 1; boardArray[1][1] = 1; boardArray[1][6] = 1; boardArray[3][3] = 1; boardArray[3][4] = 1; boardArray[4][3] = 1; boardArray[4][4] = 1; boardArray[6][1] = 1; boardArray[6][6] = 1; boardArray[7][1] = 1; boardArray[7][6] = 1; } if(boardType == 4){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ boardArray[i][j] = 0; } } //Players boardArray[1][0] = 2; boardArray[6][0] = 2; boardArray[1][7] = 3; boardArray[6][7] = 3; //Null Tiles boardArray[0][0] = 1; boardArray[0][7] = 1; boardArray[2][5] = 1; boardArray[3][4] = 1; boardArray[4][3] = 1; boardArray[5][2] = 1; boardArray[7][0] = 1; boardArray[7][7] = 1; } } public void display(){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(boardArray[i][j] == 0){ System.out.print("[ ]"); } else if(boardArray[i][j] == 1){ System.out.print("[x]"); } else if(boardArray[i][j] == 2){ System.out.print("[r]"); } else if(boardArray[i][j] == 3){ System.out.print("[b]"); } else if(boardArray[i][j] == 5){ System.out.print("[a]"); } } System.out.println(); } } public int[][] getArray(){ return boardArray; } public int[][] checkArray(int mX, int mY){ if(mX > 1 && mY > 1 && boardArray[mX - 2][mY - 2] == 0){boardArray[mX -2][mY -2] = 4;} if(mX > 1 && mY > 0 && boardArray[mX - 2][mY - 1] == 0){boardArray[mX -2][mY -1] = 4;} if(mX > 1 && boardArray[mX - 2][mY] == 0){boardArray[mX -2][mY] = 4;} if(mX > 1 && mY < 7 && boardArray[mX - 2][mY + 1] == 0){boardArray[mX -2][mY +1] = 4;} if(mX > 1 && mY < 6 && boardArray[mX - 2][mY + 2] == 0){boardArray[mX -2][mY +2] = 4;} if(mX > 0 && mY > 1 && boardArray[mX - 1][mY - 2] == 0){boardArray[mX -1][mY -2] = 4;} if(mX > 0 && mY > 0 && boardArray[mX - 1][mY - 1] == 0){boardArray[mX -1][mY -1] = 5;} if(mX > 0 && boardArray[mX - 1][mY] == 0){boardArray[mX -1][mY] = 5;} if(mX > 0 && mY < 7 && boardArray[mX - 1][mY + 1] == 0){boardArray[mX -1][mY +1] = 5;} if(mX > 0 && mY < 6 && boardArray[mX - 1][mY + 2] == 0){boardArray[mX -1][mY +2] = 4;} if(mY > 1 && boardArray[mX][mY - 2] == 0){boardArray[mX][mY -2] = 4;} if(mY > 0 && boardArray[mX][mY - 1] == 0){boardArray[mX][mY -1] = 5;} if(mY < 7 && boardArray[mX][mY + 1] == 0){boardArray[mX][mY +1] = 5;} if(mY < 6 && boardArray[mX][mY + 2] == 0){boardArray[mX][mY +2] = 4;} if(mX < 7 && mY > 1 && boardArray[mX + 1][mY - 2] == 0){boardArray[mX +1][mY -2] = 4;} if(mX < 7 && mY > 0 && boardArray[mX + 1][mY - 1] == 0){boardArray[mX +1][mY -1] = 5;} if(mX < 7 && boardArray[mX + 1][mY] == 0){boardArray[mX +1][mY] = 5;} if(mX < 7 && mY < 7 && boardArray[mX + 1][mY + 1] == 0){boardArray[mX +1][mY +1] = 5;} if(mX < 7 && mY < 6 && boardArray[mX + 1][mY + 2] == 0){boardArray[mX +1][mY +2] = 4;} if(mX < 6 && mY > 1 && boardArray[mX + 2][mY - 2] == 0){boardArray[mX +2][mY -2] = 4;} if(mX < 6 && mY > 0 && boardArray[mX + 2][mY - 1] == 0){boardArray[mX +2][mY -1] = 4;} if(mX < 6 && boardArray[mX + 2][mY] == 0){boardArray[mX +2][mY] = 4;} if(mX < 6 && mY < 7 && boardArray[mX + 2][mY + 1] == 0){boardArray[mX +2][mY +1] = 4;} if(mX < 6 && mY < 6 && boardArray[mX + 2][mY + 2] == 0){boardArray[mX +2][mY +2] = 4;} return boardArray; } public int[][] setArray(int mX, int mY, int x, int selectX, int selectY, int type){ boardArray[mX][mY] = x; if(mX > 0 && mY > 0 && (boardArray[mX-1][mY-1] == 2 || boardArray[mX-1][mY-1] == 3)){boardArray[mX-1][mY-1] = x;} if(mX > 0 && (boardArray[mX-1][mY] == 2 || boardArray[mX-1][mY] == 3)){boardArray[mX-1][mY] = x;} if(mX > 0 && mY < 7 && (boardArray[mX-1][mY+1] == 2 || boardArray[mX-1][mY+1] == 3)){boardArray[mX-1][mY+1] = x;} if(mY > 0 && (boardArray[mX][mY-1] == 2 || boardArray[mX][mY-1] == 3)){boardArray[mX][mY-1] = x;} if(mY < 7 && (boardArray[mX][mY+1] == 2 || boardArray[mX][mY+1] == 3)){boardArray[mX][mY+1] = x;} if(mX < 7 && mY > 0 && (boardArray[mX+1][mY-1] == 2 || boardArray[mX+1][mY-1] == 3)){boardArray[mX+1][mY-1] = x;} if(mX < 7 && (boardArray[mX+1][mY] == 2 || boardArray[mX+1][mY] == 3)){boardArray[mX+1][mY] = x;} if(mX < 7 && mY < 7 && (boardArray[mX+1][mY+1] == 2 || boardArray[mX+1][mY+1] == 3)){boardArray[mX+1][mY+1] = x;} if(type == 2){ boardArray[mX][mY] = x; boardArray[selectX][selectY] = 0; } return boardArray; } public void checkAdj(){ int tempMost = 0; for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(boardArray[i][j] == 4 || boardArray[i][j] == 5){ int mX = i; int mY = j; if(mX > 0 && mY > 0 && (boardArray[mX-1][mY-1] == 2 || boardArray[mX-1][mY-1] == 3)){tempMost++;} if(mX > 0 && (boardArray[mX-1][mY] == 2 || boardArray[mX-1][mY] == 3)){tempMost++;} if(mX > 0 && mY < 7 && (boardArray[mX-1][mY+1] == 2 || boardArray[mX-1][mY+1] == 3)){tempMost++;} if(mY > 0 && (boardArray[mX][mY-1] == 2 || boardArray[mX][mY-1] == 3)){tempMost++;} if(mY < 7 && (boardArray[mX][mY+1] == 2 || boardArray[mX][mY+1] == 3)){tempMost++;} if(mX < 7 && mY > 0 && (boardArray[mX+1][mY-1] == 2 || boardArray[mX+1][mY-1] == 3)){tempMost++;} if(mX < 7 && (boardArray[mX+1][mY] == 2 || boardArray[mX+1][mY] == 3)){tempMost++;} if(mX < 7 && mY < 7 && (boardArray[mX+1][mY+1] == 2 || boardArray[mX+1][mY+1] == 3)){tempMost++;} if(tempMost > curMost){ x = mX; y = mY; } } } } } public int[][] clearArray(){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(boardArray[i][j] == 4){boardArray[i][j] = 0;} if(boardArray[i][j] == 5){boardArray[i][j] = 0;} } } return boardArray; } public int checkWin(){ int p1 = 0; int p2 = 0; boolean full = true; for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(boardArray[i][j] == 0){full = false;} if(boardArray[i][j] == 2){p1++;} if(boardArray[i][j] == 3){p2++;} if(boardArray[i][j] == 4){full = false;} if(boardArray[i][j] == 5){full = false;} } } if(p1 == 0){ return 2; } else if(p2 == 0){ return 1; } else if(full == true){ if(p1 > p2){return 1;} else{return 2;} } else{ return 0; } } public void AI(){ for(int i = 0; i < 8; i++){ for(int j = 0; j < 8; j++){ if(boardArray[i][j] == 3){ checkArray(i,j); checkAdj(); } } } boardArray[x][y] = 3; setArray(x,y,3,x,y,1); clearArray(); } } Those are seperated by Classes btw.I worked on the AI at school, it now goes to EVERY spot. The only thing I really need assistance with is the Board Class's AI method and the checkAdj(); method.I know for a fact clearArray(); works as supposed and so does the checkArray method and so does the setArray(); method, i just need to figure out how to use them all together and make the AI work o.o |
|
I paid eleven minerals for THIS?
|
What you described should be extremely easy to code, just change your words into syntax. However, it sounds like a brute-force approach (check all possible moves and results and find the best outcome), which is rarely the point of writing an AI. Would a simpler (if "dumb"-er) approach be acceptable?
![]() ![]() ![]() ![]() ![]() ![]() hi
|
|
He'll spear your brains if you step out of line.
|
SEN is not the best place to get fast cheats.
I'm sure the brute force method will work fine. I doubt you had time to study the game and look for patterns that show when good moves are available. Also, your code is huge. I hope that contains the programming for the whole game, not just the AI. ![]() ![]() ![]() ![]() ![]() ![]() [quote=Felagund]The most dangerous weapon anyone can wield is self righteousness.[/quote]
I have a signature yay! |