Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
hw1p1
import java.util.Random; class Board { public int[][] pieces; public int value; public int blankRow; public int blankCol; public int lastMove; public int numMoves; public Board() { pieces = new int[3][3]; numMoves = 0; } public Board(Board b) { pieces = new int[3][3]; for (int i=0; i<3; i++) { for (int k=0; k<3; k++) { pieces[i][k] = b.pieces[i][k]; } } value = b.value; blankRow = b.blankRow; blankCol = b.blankCol; lastMove = b.lastMove; numMoves = b.numMoves; } public void printState() { for (int i=0; i<3; i++) { for (int k=0; k<3; k++) { System.out.print(pieces[i][k]+" "); } System.out.println(); } System.out.println("---- Value: "+evaluateBoard()+" ----"); } public int moveBlankUp() { if (blankRow == 0) return -1; pieces[blankRow][blankCol] = pieces[blankRow-1][blankCol]; pieces[blankRow-1][blankCol] = 0; blankRow -= 1; evaluateBoard(); lastMove = 0; numMoves++; return 0; } public int moveBlankDown() { if (blankRow == 2) return -1; pieces[blankRow][blankCol] = pieces[blankRow+1][blankCol]; pieces[blankRow+1][blankCol] = 0; blankRow += 1; evaluateBoard(); lastMove = 1; numMoves++; return 0; } public int moveBlankLeft() { if (blankCol == 0) return -1; pieces[blankRow][blankCol] = pieces[blankRow][blankCol-1]; pieces[blankRow][blankCol-1] = 0; blankCol -= 1; evaluateBoard(); lastMove = 2; numMoves++; return 0; } public int moveBlankRight() { if (blankCol == 2) return -1; pieces[blankRow][blankCol] = pieces[blankRow][blankCol+1]; pieces[blankRow][blankCol+1] = 0; blankCol += 1; evaluateBoard(); lastMove = 3; numMoves++; return 0; } public int evaluateBoard() { int score = 0; for (int i=0; i<3; i++) { for (int k=0; k<3; k++) { score += distanceFromOrigin(pieces[i][k], i, k); } } value = score; return score; } public int distanceFromOrigin(int piece, int row, int col) { int originRow = (int)Math.floor(piece/3); int originCol = piece % 3; return Math.abs(row-originRow) + Math.abs(col-originCol); } } class BoardNode { public Board board; public Board[] children; public BoardNode(Board b) { board = new Board(b); children = new Board[4]; } public void createChildren() { children = new Board[4]; for (int i=0; i<4; i++) { Board b = new Board(board); int result = 0; if (i == 0) result = b.moveBlankUp(); if (i == 1) result = b.moveBlankDown(); if (i == 2) result = b.moveBlankLeft(); if (i == 3) result = b.moveBlankRight(); if (result == 0) { b.evaluateBoard(); children[i] = b; } else children[i] = null; } } public void printChildren() { for (int i=0; i<children.length; i++) { if (children[i] != null) { children[i].printState(); } } } public int getBestMove() { int minIndex = 0; int minScore = 100000; for (int i=0; i<children.length; i++) { if (children[i] != null) { if (children[i].value < minScore) { minScore = children[i].value; minIndex = i; } } } return minIndex; } public void solvePuzzle() { while (board.value != 0 ) { createChildren(); int move = getBestMove(); if (move == 0 && board.lastMove != 1) board.moveBlankUp(); else if (move == 1 && board.lastMove != 0) board.moveBlankDown(); else if (move == 2 && board.lastMove != 3) board.moveBlankLeft(); else if (move == 3 && board.lastMove != 2) board.moveBlankRight(); else { Random randomGenerator = new Random(); int rMove = randomGenerator.nextInt(4); if (rMove == 0) board.moveBlankUp(); if (rMove == 1) board.moveBlankDown(); if (rMove == 2) board.moveBlankLeft(); if (rMove == 3) board.moveBlankRight(); } //board.printState(); //System.out.println("#####"); //printChildren(); } } } class Rextester { public static void getPiecePosition(int value) { //if (value < 0 || value > 8) return -1; int row = (int)Math.floor(value/3); int col = value % 3; System.out.println(value+": ("+row+", "+col+")"); } public static void main(String args[]) { Board b = new Board(); String s = "1,0,2,3,4,5,6,7,8"; String[] vals = s.split(","); for (int i=0; i<vals.length; i++) { int row = (int)Math.floor(i/3); int col = i % 3; b.pieces[row][col] = Integer.parseInt(vals[i]); if (vals[i].equals("0")) { b.blankRow = row; b.blankCol = col; } } BoardNode bNode = new BoardNode(b); bNode.board.printState(); bNode.solvePuzzle(); bNode.board.printState(); System.out.println("\nNumber of moves taken: "+bNode.board.numMoves); } }
run
|
edit
|
history
|
help
0
Геттеры и сеттеры для класса Dog
predecrement
MP8 game
dimond of stars
jb14.0 lyambds
191fa07050
end of bala session
BinaryTreeFindNext_noParentPointer
jb7.0 extends.override
Java