Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package conectfinal;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import conectfinal.HBrain;
- import sac.game.AlphaBetaPruning;
- import sac.game.GameSearchAlgorithm;
- import sac.game.GameState;
- import sac.game.MinMax;
- public class Connect extends sac.game.GameStateImpl {
- public static final int n = 8; // wiersze
- public static final int m = 8; // kolumny
- public static final int pustyZnak = 0; // " "
- public static final byte playerZnak = 1; // O
- public static final byte komputerZnak = 2; // X
- public int wybor = 0;
- public byte[][] board = null;
- public Connect() {
- setMaximizingTurnNow(maximizingTurnNow);
- board = new byte[n][m];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- board[i][j] = pustyZnak;
- }
- }
- }
- public Connect(Connect x) {
- setMaximizingTurnNow(x.isMaximizingTurnNow());
- board = new byte[n][m];
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- this.board[i][j] = x.board[i][j];
- }
- }
- }
- public boolean insert(int x) {
- if (x > m || x <= 0)
- return false;
- x = x - 1;
- for (int i = 0; i < n; i++) {
- if (board[(n - 1) - i][x] == pustyZnak) {
- if (isMaximizingTurnNow()) {
- board[(n - 1) - i][x] = playerZnak;
- } else {
- board[(n - 1) - i][x] = komputerZnak;
- }
- setMaximizingTurnNow(!isMaximizingTurnNow());
- return true;
- }
- }
- return false;
- }
- public boolean check(int x) {
- int win = 0;
- // poziom
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (x == 1) {
- if (board[i][j] == 1)
- win++;
- else
- win = 0;
- if (win == 4)
- return true;
- }
- if (x == 2) {
- if (board[i][j] == 2)
- win++;
- else
- win = 0;
- if (win == 4)
- return true;
- }
- }
- win = 0;
- }
- win = 0;
- // pion
- for (int i = 0; i < m; i++) {
- for (int j = 0; j < n; j++) {
- if (x == 1) {
- if (board[j][i] == 1)
- win++;
- else
- win = 0;
- if (win == 4)
- return true;
- }
- if (x == 2) {
- if (board[j][i] == 2)
- win++;
- else
- win = 0;
- if (win == 4)
- return true;
- }
- }
- win = 0;
- }
- // skos z gory na prawo
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (x == 1) {
- if (board[i][j] == 1)
- if (i + 3 < n && j + 3 < m)
- if (board[i + 1][j + 1] == 1)
- if (board[i + 2][j + 2] == 1)
- if (board[i + 3][j + 3] == 1)
- return true;
- }
- if (x == 2) {
- if (board[i][j] == 2)
- if (i + 3 < n && j + 3 < m)
- if (board[i + 1][j + 1] == 2)
- if (board[i + 2][j + 2] == 2)
- if (board[i + 3][j + 3] == 2)
- return true;
- }
- }
- }
- // skos z gory na lewo
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (x == 1) {
- if (board[i][j] == 1)
- if (i + 3 < n && j - 3 >= 0)
- if (board[i + 1][j - 1] == 1)
- if (board[i + 2][j - 2] == 1)
- if (board[i + 3][j - 3] == 1)
- return true;
- }
- if (board[i][j] == 2)
- if (i + 3 < n && j - 3 >= 0)
- if (board[i + 1][j - 1] == 2)
- if (board[i + 2][j - 2] == 2)
- if (board[i + 3][j - 3] == 2)
- return true;
- }
- }
- // sufit
- for (int i = 0; i < m; i++) {
- if (x == 1) {
- if (board[0][i] == 1)
- return true;
- }
- if (x == 2)
- if (board[0][i] == 2)
- return true;
- }
- return false;
- }
- @Override
- public String toString() {
- String show = "";
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (board[i][j] == pustyZnak)
- show += " " + "|";
- else if (board[i][j] == playerZnak)
- show += "O" + "|";
- else if (board[i][j] == komputerZnak)
- show += "X" + "|";
- }
- show += "\n";
- }
- for (int i = 0; i < m; i++) {
- show += (i + 1) + " ";
- }
- return show;
- }
- @Override
- public List<GameState> generateChildren() {
- List<GameState> children = new ArrayList<GameState>();
- for (int i = 0; i < n; i++) {
- Connect child = new Connect(this);
- if (child.insert(i)) {
- child.setMoveName("" + i);
- children.add(child);
- }
- }
- return children;
- }
- @Override
- public int hashCode() {
- return toString().hashCode();
- }
- public static void main(String[] args) {
- Connect a = new Connect();
- Connect.setHFunction(new HBrain());
- GameSearchAlgorithm alg = new AlphaBetaPruning(a);
- int x;
- String y;
- Scanner scanIn = new Scanner(System.in);
- System.out.println("Kto zaczyna ?");
- System.out.println("1: Gracz");
- System.out.println("2: Komputer");
- a.wybor = Integer.parseInt(scanIn.nextLine());
- System.out.println(a);
- if (a.wybor == 1) {
- for (;;) {
- System.out.println();
- System.out.print("Podaj numer kolumny ");
- x = Integer.parseInt(scanIn.nextLine());
- a.insert(x);
- if (a.check(1)) {
- System.out.println("Gratulacje, wygrales !");
- break;
- }
- alg.execute();
- System.out.println(alg.getMovesScores());
- x = Integer.parseInt(alg.getFirstBestMove());
- a.insert(x);
- System.out.println(new HBrain().calculate(a));
- System.out.println(a);
- if (a.check(2)) {
- System.out.println("Przegrales, sprobuj jeszcze raz :) !");
- break;
- }
- }
- }
- if (a.wybor == 2) {
- for (;;) {
- alg.execute();
- System.out.println(alg.getMovesScores());
- x = Integer.parseInt(alg.getFirstBestMove());
- a.insert(x);
- System.out.println(new HBrain().calculate(a));
- System.out.println(a);
- if (a.check(1)) {
- System.out.println("Przegrales, sprobuj jeszcze raz :) !");
- break;
- }
- System.out.println();
- System.out.print("Podaj numer kolumny ");
- x = Integer.parseInt(scanIn.nextLine());
- a.insert(x);
- if (a.check(2)) {
- System.out.println(a);
- System.out.println("Gratulacje, wygrales !");
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement