Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedList;
- final int BoardSize = 8; // размер доски в квадратах
- final float SquareSize = 50; // размер квадрата в пикселях
- final float PieceSize = 0.6 * SquareSize; // диаметр шашки
- final color BgColor = color(100); // цвет фона
- PlayerT whitePlayerT = PlayerT.Person;
- PlayerT blackPlayerT = PlayerT.Bot;
- // Загрузить расстановку шашек из редактора или расставить стандартно
- boolean loadGameFromEditor = false;
- Menu menu;
- Game game;
- Editor editor;
- Scene currentScene;
- enum PlayerT {
- Person,
- Bot
- }
- static class Vec2Int {
- final int x;
- final int y;
- Vec2Int(int x, int y) {
- this.x = x;
- this.y = y;
- }
- Vec2Int add(Vec2Int v) {
- return new Vec2Int(x + v.x, y + v.y);
- }
- boolean equals(Vec2Int v) {
- return x == v.x && y == v.y;
- }
- }
- enum Dir {
- DownRight(1, 1), DownLeft(-1, 1), UpLeft(-1, -1), UpRight(1, -1);
- final Vec2Int vec;
- private Dir(int x, int y) {
- vec = new Vec2Int(x, y);
- }
- Dir left() {
- Dir[] vals = values();
- int i = ordinal() - 1;
- if (i < 0) i += vals.length;
- return vals[i];
- }
- Dir right() {
- Dir[] vals = values();
- return vals[(ordinal() + 1) % vals.length];
- }
- Dir[] perpendiculars() {
- return new Dir[] {left(), right()};
- }
- }
- Dir[] forwardDirs(Color c) {
- if (c == Color.White) {
- return new Dir[] {Dir.UpLeft, Dir.UpRight};
- } else {
- return new Dir[] {Dir.DownRight, Dir.DownLeft};
- }
- }
- Dir[] backwardDirs(Color c) {
- return forwardDirs(c.getInverse());
- }
- enum Color {
- White {
- Color getInverse() {
- return Black;
- }
- },
- Black {
- Color getInverse() {
- return White;
- }
- };
- abstract Color getInverse();
- }
- int lastRow(Color col) {
- if (col == Color.White) return 0;
- return BoardSize - 1;
- }
- // хранит информацию о ходе
- // направление движения
- // квадрат, в который встает шашка
- // ссылка на шашку, которая бьется в этом ходу
- class Move {
- final Dir direction;
- final Vec2Int position;
- final Piece capturedPiece;
- Move(Dir dir, Vec2Int pos, Piece piece) {
- direction = dir;
- position = pos;
- capturedPiece = piece;
- }
- Move(Dir dir, Vec2Int pos) {
- this(dir, pos, null);
- }
- }
- class Piece {
- Vec2Int position;
- Color col;
- boolean king = false; // дамка
- boolean captured = false; // битая
- ArrayList<Move> moves = new ArrayList<Move>(); // ходы без взятия
- ArrayList<Move> captures = new ArrayList<Move>(); // ходы со взятием
- Board board;
- Piece(Vec2Int position, Color col, boolean king) {
- this.position = position;
- this.col = col;
- this.king = king;
- }
- void findMovesAndCaptures() {
- moves.clear();
- captures.clear();
- if (king) {
- king_findMovesAndCaptures();
- } else {
- man_findMovesAndCaptures();
- }
- }
- void man_findMovesAndCaptures() {
- for (Dir d: forwardDirs(col)) {
- Vec2Int pos = position.add(d.vec);
- if (!checkSquarePosition(pos)) continue;
- Piece otherPiece = board.getPiece(pos);
- if (otherPiece == null) {
- moves.add(new Move(d, pos));
- } else if (otherPiece.col != col && !otherPiece.captured) {
- pos = pos.add(d.vec);
- if (checkSquarePosition(pos) && board.getPiece(pos) == null) {
- captures.add(new Move(d, pos, otherPiece));
- }
- }
- }
- for (Dir d: backwardDirs(col)) {
- Vec2Int pos = position.add(d.vec);
- if (!checkSquarePosition(pos)) continue;
- Piece otherPiece = board.getPiece(pos);
- if (otherPiece != null && otherPiece.col != col && !otherPiece.captured) {
- pos = pos.add(d.vec);
- if (checkSquarePosition(pos) && board.getPiece(pos) == null) {
- captures.add(new Move(d, pos, otherPiece));
- }
- }
- }
- }
- void king_findMovesAndCaptures() {
- for (Dir d: Dir.values()) {
- Vec2Int pos = position;
- while (true) {
- pos = pos.add(d.vec);
- if (!checkSquarePosition(pos)) break;
- Piece otherPiece = board.getPiece(pos);
- if (otherPiece == null) {
- moves.add(new Move(d, pos));
- } else {
- if (otherPiece.col != col && !otherPiece.captured) {
- Piece capturedPiece = otherPiece;
- pos = pos.add(d.vec);
- ArrayList<Move> singleCaptures = new ArrayList<Move>();
- ArrayList<Move> doubleCaptures = new ArrayList<Move>();
- while (checkSquarePosition(pos) && board.getPiece(pos) == null) {
- Move move = new Move(d, pos, capturedPiece);
- singleCaptures.add(move);
- L:
- for (Dir perpD: d.perpendiculars()) {
- Vec2Int perpPos = pos;
- while (true) {
- perpPos = perpPos.add(perpD.vec);
- if (!checkSquarePosition(perpPos)) break;
- otherPiece = board.getPiece(perpPos);
- if (otherPiece != null) {
- if (otherPiece.col != col && !otherPiece.captured) {
- perpPos = perpPos.add(perpD.vec);
- if (checkSquarePosition(perpPos) && board.getPiece(perpPos) == null) {
- doubleCaptures.add(move);
- break L;
- }
- }
- break;
- }
- }
- }
- pos = pos.add(d.vec);
- }
- if (doubleCaptures.size() > 0) {
- captures.addAll(doubleCaptures);
- } else {
- captures.addAll(singleCaptures);
- }
- }
- break;
- }
- }
- }
- }
- // есть ли среди найденных ходов ход в текущую клетку
- Move getMove(Vec2Int position, boolean capturesFound) {
- for (Move move: getMoveList(capturesFound)) {
- if (move.position.equals(position)) {
- return move;
- }
- }
- return null;
- }
- ArrayList<Move> getMoveList(boolean capturesFound) {
- if (capturesFound) {
- return captures;
- } else {
- return moves;
- }
- }
- }
- class Board {
- Piece[][] squares = new Piece[BoardSize][BoardSize];
- LinkedList<Piece> whitePieces = new LinkedList<Piece>();
- LinkedList<Piece> blackPieces = new LinkedList<Piece>();
- void addPiece(int x, int y, Color c, boolean king) {
- if (squares[x][y] != null) throw new Error();
- Piece piece = new Piece(new Vec2Int(x, y), c, king);
- piece.board = this;
- squares[x][y] = piece;
- getPieces(c).add(piece);
- }
- void addPiece(int x, int y, Color c) {
- addPiece(x, y, c, false);
- }
- Piece getPiece(Vec2Int position) {
- return squares[position.x][position.y];
- }
- void removePiece(Piece piece) {
- getPieces(piece.col).remove(piece);
- squares[piece.position.x][piece.position.y] = null;
- }
- void movePiece(Piece piece, Vec2Int position) {
- if (squares[position.x][position.y] != null) throw new Error();
- squares[piece.position.x][piece.position.y] = null;
- piece.position = position;
- squares[piece.position.x][piece.position.y] = piece;
- }
- LinkedList<Piece> getPieces(Color c) {
- if (c == Color.White) {
- return whitePieces;
- } else {
- return blackPieces;
- }
- }
- }
- class Button {
- int x;
- int y;
- int w;
- int h = 30;
- String text;
- boolean pressed = false;
- boolean enabled = true;
- Button(int x, int y, int w, String t) {
- this.x = x;
- this.y = y;
- this.w = w;
- this.text = t;
- }
- void draw() {
- if (!enabled) return;
- noStroke();
- if (mouseOver()) {
- fill(200);
- } else {
- fill(150);
- }
- rect(x, y, w, h, 5);
- fill(0);
- textSize(16);
- textAlign(CENTER, CENTER);
- text(text, x + w / 2, y + h / 2);
- }
- void mousePressed() {
- if (!enabled) return;
- if (mouseOver()) {
- pressed = true;
- }
- }
- boolean mouseOver() {
- if (!enabled) return false;
- return mouseX >= x && mouseX < (x + w - 1) && mouseY >= y && mouseY < (y + h - 1);
- }
- }
- class ButtonGroup {
- ArrayList<Button> buttons = new ArrayList<Button>();
- void add(Button b) {
- buttons.add(b);
- }
- void draw() {
- for (Button b: buttons) {
- b.draw();
- }
- }
- void mousePressed() {
- for (Button b: buttons) {
- b.mousePressed();
- }
- }
- }
- interface Scene {
- void init();
- void draw();
- void mousePressed();
- }
- class Menu implements Scene {
- ButtonGroup buttons = new ButtonGroup();
- Button playWhiteBtn;
- Button playBlackBtn;
- Button twoPersonsBtn;
- Button twoBotsBtn;
- Button editorBtn;
- Menu() {
- int bX = 20;
- int bW = 180;
- playWhiteBtn = new Button(bX, 100, bW, "Играть за белых");
- playBlackBtn = new Button(bX, 140, bW, "Играть за черных");
- twoPersonsBtn = new Button(bX, 180, bW, "Играть вдвоем");
- twoBotsBtn = new Button(bX, 220, bW, "Игра ботов");
- editorBtn = new Button(bX, 260, bW, "Редактор");
- buttons.add(playWhiteBtn);
- buttons.add(playBlackBtn);
- buttons.add(twoPersonsBtn);
- buttons.add(twoBotsBtn);
- buttons.add(editorBtn);
- }
- void init() {
- }
- void draw() {
- background(BgColor);
- fill(0);
- textAlign(CENTER);
- textSize(32);
- text("Русские Шашки", width / 2, 50);
- buttons.draw();
- }
- void mousePressed() {
- loadGameFromEditor = false;
- if (playWhiteBtn.mouseOver()) {
- whitePlayerT = PlayerT.Person;
- blackPlayerT = PlayerT.Bot;
- loadScene(game);
- } else if (playBlackBtn.mouseOver()) {
- whitePlayerT = PlayerT.Bot;
- blackPlayerT = PlayerT.Person;
- loadScene(game);
- } else if (twoPersonsBtn.mouseOver()) {
- whitePlayerT = PlayerT.Person;
- blackPlayerT = PlayerT.Person;
- loadScene(game);
- } else if (twoBotsBtn.mouseOver()) {
- whitePlayerT = PlayerT.Bot;
- blackPlayerT = PlayerT.Bot;
- loadScene(game);
- } else if (editorBtn.mouseOver()) {
- loadScene(editor);
- }
- }
- }
- class GameState {
- void init() {};
- void update() {};
- void mousePressed(Vec2Int currentSquare) {};
- }
- class Game implements Scene {
- Board board;
- ArrayList<Piece> capturedPieces; // битые шашки
- ArrayList<Piece> availablePieces; // доступные для очередного хода шашки
- ArrayList<Vec2Int> lastMoves; // ход последней шашки
- ArrayList<Vec2Int> lastCapturedPieces; // и сбитые ей шашки
- boolean gameFinished;
- Color winner;
- // устанавливаются в setPlayer()
- Color currentPlayer;
- Piece selectedPiece;
- Move selectedMove;
- boolean capturesFound; // если обнаружены ходы со взятием, то выбирать можно только из них
- boolean multipleCapture; // последовательное взятие
- final int FramesPerSquare = 5; // скорость перемещения шашки на одну клетку
- float movedPieceProgress; // пройденная дистанция шашки (1 клетка - 1 единица)
- GameState st_PersonInput = new State_PersonInput();
- GameState st_BotInput = new State_BotInput();
- GameState st_MovePiece = new State_MovePiece();
- GameState currentState;
- ButtonGroup buttons = new ButtonGroup();
- Button menuBtn;
- Button resetBtn;
- Button editorBtn;
- Game() {
- menuBtn = new Button(20, height - 40, 100, "Меню");
- resetBtn = new Button(140, height - 40, 160, "Перезапустить");
- editorBtn = new Button(320, height - 40, 120, "Редактор");
- buttons.add(menuBtn);
- buttons.add(resetBtn);
- buttons.add(editorBtn);
- }
- void init() {
- board = new Board();
- capturedPieces = new ArrayList<Piece>();
- availablePieces = new ArrayList<Piece>();
- lastMoves = new ArrayList<Vec2Int>();
- lastCapturedPieces = new ArrayList<Vec2Int>();
- gameFinished = false;
- if (loadGameFromEditor) { // загрузить расстановку шашек из редактора
- editorBtn.enabled = true;
- for (int x = 0; x < BoardSize; ++x) {
- for (int y = 0; y < BoardSize; ++y) {
- switch (editor.boardData[x][y]) {
- case Editor.WhiteMan:
- board.addPiece(x, y, Color.White);
- break;
- case Editor.WhiteKing:
- board.addPiece(x, y, Color.White, true);
- break;
- case Editor.BlackMan:
- board.addPiece(x, y, Color.Black);
- break;
- case Editor.BlackKing:
- board.addPiece(x, y, Color.Black, true);
- break;
- }
- }
- }
- } else { // расставить шашки стандартно
- editorBtn.enabled = false;
- Color c = Color.Black;
- board.addPiece(1, 0, c);
- board.addPiece(3, 0, c);
- board.addPiece(5, 0, c);
- board.addPiece(7, 0, c);
- board.addPiece(0, 1, c);
- board.addPiece(2, 1, c);
- board.addPiece(4, 1, c);
- board.addPiece(6, 1, c);
- board.addPiece(1, 2, c);
- board.addPiece(3, 2, c);
- board.addPiece(5, 2, c);
- board.addPiece(7, 2, c);
- c = Color.White;
- board.addPiece(0, 5, c);
- board.addPiece(2, 5, c);
- board.addPiece(4, 5, c);
- board.addPiece(6, 5, c);
- board.addPiece(1, 6, c);
- board.addPiece(3, 6, c);
- board.addPiece(5, 6, c);
- board.addPiece(7, 6, c);
- board.addPiece(0, 7, c);
- board.addPiece(2, 7, c);
- board.addPiece(4, 7, c);
- board.addPiece(6, 7, c);
- }
- setPlayer(Color.White);
- setPlayerInputState();
- }
- void draw() {
- if (!gameFinished) {
- currentState.update();
- }
- background(BgColor);
- drawBoard();
- // предыдущий ход
- for (int i = 0; i < lastMoves.size() - 1; ++i) {
- stroke(0, 0, 255);
- strokeWeight(2);
- Vec2Int p1 = lastMoves.get(i);
- Vec2Int p2 = lastMoves.get(i + 1);
- line((p1.x + 0.5) * SquareSize, (p1.y + 0.5) * SquareSize, (p2.x + 0.5) * SquareSize, (p2.y + 0.5) * SquareSize);
- }
- for (Vec2Int p: lastCapturedPieces) {
- noStroke();
- fill(0, 0, 255, 100);
- ellipse((p.x + 0.5) * SquareSize, (p.y + 0.5) * SquareSize, PieceSize, PieceSize);
- }
- // шашки
- for (int x = 0; x < BoardSize; ++x) {
- for (int y = 0; y < BoardSize; ++y) {
- Piece piece = board.squares[x][y];
- if (piece != null) {
- // если идет анимащия хода шашки, то эта шашка рисуется отдельно
- if (selectedMove != null && piece == selectedPiece) {
- continue;
- }
- drawPiece(x, y, piece.col == Color.White, piece.king);
- if (piece.captured) {
- stroke(255, 0, 0);
- strokeWeight(2);
- float crossX1 = x * SquareSize;
- float crossY1 = y * SquareSize;
- float crossX2 = crossX1 + SquareSize;
- float crossY2 = crossY1 + SquareSize;
- line(crossX1, crossY1, crossX2, crossY2);
- line(crossX2, crossY1, crossX1, crossY2);
- }
- }
- }
- }
- // выделение шашек, которыми можно ходить
- if (!multipleCapture && selectedMove == null) {
- if (currentPlayerType() == PlayerT.Person) {
- stroke(255, 255, 0);
- } else {
- stroke(200, 0, 255);
- }
- strokeWeight(2);
- noFill();
- for (Piece piece: board.getPieces(currentPlayer)) {
- boolean pieceAvailable = false;
- if (capturesFound) {
- pieceAvailable = piece.captures.size() > 0;
- } else {
- pieceAvailable = piece.moves.size() > 0;
- }
- if (pieceAvailable) {
- rect(piece.position.x * SquareSize, piece.position.y * SquareSize, SquareSize, SquareSize);
- }
- }
- }
- // выделение выбранной шашки
- if (selectedPiece != null) {
- noFill();
- stroke(0, 255, 0);
- strokeWeight(2);
- rect(selectedPiece.position.x * SquareSize, selectedPiece.position.y * SquareSize, SquareSize, SquareSize);
- for (Move move: selectedPiece.getMoveList(capturesFound)) {
- ellipse((move.position.x + 0.5) * SquareSize, (move.position.y + 0.5) * SquareSize, PieceSize, PieceSize);
- }
- }
- // анимация хода шашки
- if (selectedMove != null) {
- pushMatrix();
- Dir d = selectedMove.direction;
- float dist = movedPieceProgress * SquareSize;
- translate(d.vec.x * dist, d.vec.y * dist);
- Vec2Int pos = selectedPiece.position;
- drawPiece(pos.x, pos.y, selectedPiece.col == Color.White, selectedPiece.king);
- popMatrix();
- }
- // UI
- drawPiece(BoardSize + 1, 1, false, false);
- drawPiece(BoardSize + 1, 2, true, false);
- fill(0);
- textSize(32);
- textAlign(CENTER, CENTER);
- text(Integer.toString(board.blackPieces.size()), (BoardSize + 2.5) * SquareSize, 1.5 * SquareSize);
- text(Integer.toString(board.whitePieces.size()), (BoardSize + 2.5) * SquareSize, 2.5 * SquareSize);
- if (gameFinished) {
- textAlign(CENTER, TOP);
- if (winner == Color.White) {
- text("Победили белые", (BoardSize / 2) * SquareSize, BoardSize * SquareSize + 10);
- } else {
- text("Победили черные", (BoardSize / 2) * SquareSize, BoardSize * SquareSize + 10);
- }
- }
- buttons.draw();
- }
- void mousePressed() {
- if (!gameFinished && mouseButton == LEFT) {
- Vec2Int square = new Vec2Int(floor(mouseX / SquareSize), floor(mouseY / SquareSize));
- if (checkSquarePosition(square)) {
- currentState.mousePressed(square);
- }
- }
- if (menuBtn.mouseOver()) {
- loadScene(menu);
- } else if (resetBtn.mouseOver()) {
- loadScene(game);
- } else if (editorBtn.mouseOver()) {
- loadScene(editor);
- }
- }
- void markCaptured(Piece piece) {
- piece.captured = true;
- capturedPieces.add(piece);
- }
- void movePiece(Piece piece, Vec2Int position) {
- board.movePiece(piece, position);
- if (piece.position.y == lastRow(piece.col)) {
- piece.king = true;
- }
- }
- void setPlayer(Color c) {
- currentPlayer = c;
- selectedPiece = null;
- selectedMove = null;
- multipleCapture = false;
- capturesFound = false;
- for (Piece piece: board.getPieces(c)) {
- piece.findMovesAndCaptures();
- if (piece.captures.size() > 0) {
- capturesFound = true;
- }
- }
- availablePieces.clear();
- for (Piece piece: board.getPieces(c)) {
- if (piece.getMoveList(capturesFound).size() > 0) {
- availablePieces.add(piece);
- }
- }
- if (availablePieces.size() == 0) {
- gameFinished = true;
- winner = c.getInverse();
- }
- }
- void switchPlayer() {
- for (Piece piece: capturedPieces) {
- lastCapturedPieces.add(piece.position);
- board.removePiece(piece);
- }
- capturedPieces.clear();
- setPlayer(currentPlayer.getInverse());
- }
- void setState(GameState state) {
- currentState = state;
- currentState.init();
- }
- PlayerT currentPlayerType() {
- return currentPlayer == Color.White ? whitePlayerT : blackPlayerT;
- }
- void setPlayerInputState() {
- if (currentPlayerType() == PlayerT.Person) {
- setState(st_PersonInput);
- } else {
- setState(st_BotInput);
- }
- }
- class State_PersonInput extends GameState {
- @Override
- void mousePressed(Vec2Int currentSquare) {
- if (multipleCapture) { // можно только походить выбранной шашкой
- Move availableMove = selectedPiece.getMove(currentSquare, true);
- if (availableMove != null) {
- selectedMove = availableMove;
- setState(st_MovePiece);
- }
- } else { // можно выбрать шашку, сменить выбранную шашку, походить
- Piece piece = board.getPiece(currentSquare);
- if (piece != null) {
- if (piece.col == currentPlayer && piece.getMoveList(capturesFound).size() > 0) {
- selectedPiece = piece;
- }
- } else if (selectedPiece != null) {
- Move availableMove = selectedPiece.getMove(currentSquare, capturesFound);
- if (availableMove != null) {
- selectedMove = availableMove;
- setState(st_MovePiece);
- }
- }
- }
- }
- }
- class State_BotInput extends GameState {
- final int delay = 40;
- int time;
- @Override()
- void init() {
- time = 0;
- }
- @Override
- void update() {
- ++time;
- if (time > delay) {
- if (selectedPiece == null) { // выбрать шашку
- int i = floor(random(availablePieces.size()));
- selectedPiece = availablePieces.get(i);
- time = 0;
- } else { // выбрать ход
- ArrayList<Move> availableMoves = selectedPiece.getMoveList(capturesFound);
- int i = floor(random(availableMoves.size()));
- selectedMove = availableMoves.get(i);
- setState(st_MovePiece);
- }
- }
- }
- }
- class State_MovePiece extends GameState {
- int time;
- int duration;
- @Override
- void init() {
- time = 0;
- duration = abs(selectedMove.position.x - selectedPiece.position.x) * FramesPerSquare;
- if (!multipleCapture) {
- lastMoves.clear();
- lastMoves.add(selectedPiece.position);
- lastCapturedPieces.clear();
- }
- lastMoves.add(selectedMove.position);
- }
- @Override
- void update() {
- ++time;
- if (time < duration) {
- movedPieceProgress = (float)time / FramesPerSquare;
- } else {
- movedPieceProgress = 0;
- movePiece(selectedPiece, selectedMove.position);
- if (selectedMove.capturedPiece != null) {
- markCaptured(selectedMove.capturedPiece);
- selectedPiece.findMovesAndCaptures();
- if (selectedPiece.captures.size() == 0) {
- switchPlayer();
- } else {
- multipleCapture = true;
- }
- } else {
- switchPlayer();
- }
- selectedMove = null;
- setPlayerInputState();
- }
- }
- }
- }
- class Editor implements Scene {
- static final int Empty = 0;
- static final int WhiteMan = 1;
- static final int WhiteKing = 2;
- static final int BlackMan = 3;
- static final int BlackKing = 4;
- static final int PieceTypeNum = 5;
- int boardData[][] = new int[BoardSize][BoardSize];
- int pieceType = WhiteMan;
- int pieceTypeSquareX = BoardSize + 1;
- ButtonGroup buttons = new ButtonGroup();
- Button menuBtn;
- Button testBtn;
- Editor() {
- menuBtn = new Button(20, height - 40, 100, "Меню");
- testBtn = new Button(140, height - 40 , 100, "Тест");
- buttons.add(menuBtn);
- buttons.add(testBtn);
- }
- void init() {
- }
- void draw() {
- background(BgColor);
- drawBoard();
- for (int x = 0; x < BoardSize; ++x) {
- for (int y = 0; y < BoardSize; ++y) {
- switch (boardData[x][y]) {
- case WhiteMan:
- drawPiece(x, y, true, false);
- break;
- case WhiteKing:
- drawPiece(x, y, true, true);
- break;
- case BlackMan:
- drawPiece(x, y, false, false);
- break;
- case BlackKing:
- drawPiece(x, y, false, true);
- break;
- }
- }
- }
- drawPiece(pieceTypeSquareX, WhiteMan, true, false);
- drawPiece(pieceTypeSquareX, WhiteKing, true, true);
- drawPiece(pieceTypeSquareX, BlackMan, false, false);
- drawPiece(pieceTypeSquareX, BlackKing, false, true);
- stroke(0, 255, 0);
- strokeWeight(2);
- noFill();
- rect(pieceTypeSquareX * SquareSize, pieceType * SquareSize, SquareSize, SquareSize);
- fill(0);
- textSize(16);
- textAlign(LEFT, TOP);
- text("ЛКМ - поставить шашку", 20, BoardSize * SquareSize + 10);
- text("ПКМ - убрать шашку", 20, BoardSize * SquareSize + 30);
- buttons.draw();
- }
- void mousePressed() {
- Vec2Int currentSquare = new Vec2Int(floor(mouseX / SquareSize), floor(mouseY / SquareSize));
- if (checkSquarePosition(currentSquare)) {
- if (mouseButton == LEFT) {
- boardData[currentSquare.x][currentSquare.y] = pieceType;
- } else if (mouseButton == RIGHT) {
- boardData[currentSquare.x][currentSquare.y] = Empty;
- }
- } else if (currentSquare.x == pieceTypeSquareX && currentSquare.y > 0 && currentSquare.y < PieceTypeNum) {
- pieceType = currentSquare.y;
- }
- if (menuBtn.mouseOver()) {
- loadScene(menu);
- } else if (testBtn.mouseOver()) {
- loadGameFromEditor = true;
- whitePlayerT = PlayerT.Person;
- blackPlayerT = PlayerT.Person;
- loadScene(game);
- }
- }
- }
- void setup() {
- size(600, 500);
- menu = new Menu();
- game = new Game();
- editor = new Editor();
- loadScene(menu);
- }
- void draw() {
- currentScene.draw();
- }
- void mousePressed() {
- currentScene.mousePressed();
- }
- void loadScene(Scene s) {
- currentScene = s;
- currentScene.init();
- }
- boolean checkSquarePosition(Vec2Int p) {
- return p.x >= 0 && p.x < BoardSize && p.y >= 0 && p.y < BoardSize;
- }
- void drawBoard() {
- for (int x = 0; x < BoardSize; ++x) {
- for (int y = 0; y < BoardSize; ++y) {
- if (x % 2 != y % 2) {
- fill(150);
- } else {
- fill(220);
- }
- noStroke();
- rect(x * SquareSize, y * SquareSize, SquareSize, SquareSize);
- }
- }
- }
- void drawPiece(int x, int y, boolean white, boolean king) {
- stroke(0);
- strokeWeight(2);
- fill(white ? 255 : 0);
- float centerX = (x + 0.5) * SquareSize;
- float centerY = (y + 0.5) * SquareSize;
- ellipse(centerX, centerY, PieceSize, PieceSize);
- if (king) {
- noStroke();
- fill(white ? 0 : 255);
- float dotSize = PieceSize * 0.3;
- ellipse(centerX, centerY, dotSize, dotSize);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement