Skip to content

Instantly share code, notes, and snippets.

@ornirus
Last active October 24, 2016 13:51
Show Gist options
  • Save ornirus/f7145e7899819bbd498441f22a143677 to your computer and use it in GitHub Desktop.
Save ornirus/f7145e7899819bbd498441f22a143677 to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class GUI extends JFrame
{
private JButton start;
private JButton roll;
static int[][] grid = new int[][]{
{2, 1, 1, 1, 1, 1, 2, 4},
{1, 1, 0, 2, 0, 3, 3, 3},
{1, 1, 1, 11, 0, 0, 3, 3},
{1, 1, 0, 1, 0, 0, 13, 3},
{11, 1, 0, 1, 0, 0, 3, 3},
{1, 1, 0, 3, 3, 3, 3, 3},
{1, 1, 0, 0, 10, 0, 3, 3},
{0, 0, 0, 0, 0, 0, 0, 0},
};
private JPanel panel;
private Random random;
int players = 4;
int playerTurn = 0;
int xMove;
int yMove;
public Player[] pn = new Player[4];
public void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
random = new Random();
panel = new JPanel();
panel.setPreferredSize(new Dimension(809, 809));
panel.setBackground(Color.lightGray);
window.add(panel);
start = new JButton("Start");
window.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
//askPlayers();
start.setVisible(false);
roll.setVisible(true);
//render();
//turn(playerTurn);
}
});
roll = new JButton("Roll");
window.add(roll);
roll.setVisible(false);
roll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
render();
pn[playerTurn].draw(paper);
roll();
//move();
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
turn(playerTurn);
}
});
}
public void render(){
BufferedImage board = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper = board.getGraphics();
paper.setColor(Color.lightGray);
paper.fillRect(0, 0, 809, 809);
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
paper.setColor(Color.white);
paper.fillRect(1 + 101 * i, 1 + 101 * j, 100, 100);
if (grid[j][i]/10%10 == 1) {
paper.setColor(Color.yellow);
paper.fillRect(1 + 101 * i, 1 + 101 * j, 100, 100);
}
/*if (grid[j][i]/100 == 1) {
paper.setColor(Color.red);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
if (grid[j][i]/100 == 2) {
paper.setColor(Color.green);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
if (grid[j][i]/100 == 3) {
paper.setColor(Color.blue);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
if (grid[j][i]/100 == 4) {
paper.setColor(Color.orange);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
paper.setColor(Color.black);
if (grid[j][i]%10 == 0) {
paper.fillRect(46 + 101 * i,11 + 101 * j, 10, 80);
paper.fillRect(36 + 101 * i,21 + 101 * j, 30, 10);
paper.fillRect(26 + 101 * i,31 + 101 * j, 50, 10);
}
if (grid[j][i]%10 == 1) {
paper.fillRect(11 + 101 * i,46 + 101 * j, 80, 10);
paper.fillRect(71 + 101 * i,36 + 101 * j, 10, 30);
paper.fillRect(61 + 101 * i,26 + 101 * j, 10, 50);
}
if (grid[j][i]%10 == 2) {
paper.fillRect(46 + 101 * i,11 + 101 * j, 10, 80);
paper.fillRect(36 + 101 * i,71 + 101 * j, 30, 10);
paper.fillRect(26 + 101 * i,61 + 101 * j, 50, 10);
}
if (grid[j][i]%10 == 3) {
paper.fillRect(11 + 101 * i,46 + 101 * j, 80, 10);
paper.fillRect(21 + 101 * i, 36 + 101 * j, 10, 30);
paper.fillRect(31 + 101 * i,26 + 101 * j, 10, 50);
}*/
boolean white = false;
if (grid[j][i]%10 == 4) {
for (int x = 0; x < 10; x++) {
if (white) {
paper.setColor(Color.white);
white = false;
}
else {
paper.setColor(Color.black);
white = true;
}
for (int y = 0; y < 10; y++) {
if (white) {
paper.setColor(Color.white);
white = false;
}
else {
paper.setColor(Color.black);
white = true;
}
paper.fillRect(1 + 101 * i + x * 10, 1 + 101 * j + y * 10, 10, 10);
}
}
}
}
}
panel.getGraphics().drawImage(board, 0, 0, this);
}
public void drawArrows() {
paper.setColor(Color.black);
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[j][i]%10 == 0) {
paper.fillRect(46 + 101 * i,11 + 101 * j, 10, 80);
paper.fillRect(36 + 101 * i,21 + 101 * j, 30, 10);
paper.fillRect(26 + 101 * i,31 + 101 * j, 50, 10);
}
if (grid[j][i]%10 == 1) {
paper.fillRect(11 + 101 * i,46 + 101 * j, 80, 10);
paper.fillRect(71 + 101 * i,36 + 101 * j, 10, 30);
paper.fillRect(61 + 101 * i,26 + 101 * j, 10, 50);
}
if (grid[j][i]%10 == 2) {
paper.fillRect(46 + 101 * i,11 + 101 * j, 10, 80);
paper.fillRect(36 + 101 * i,71 + 101 * j, 30, 10);
paper.fillRect(26 + 101 * i,61 + 101 * j, 50, 10);
}
if (grid[j][i]%10 == 3) {
paper.fillRect(11 + 101 * i,46 + 101 * j, 80, 10);
paper.fillRect(21 + 101 * i, 36 + 101 * j, 10, 30);
paper.fillRect(31 + 101 * i,26 + 101 * j, 10, 50);
}
}
}
}
public void roll() {
BufferedImage roll = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper2 = roll.getGraphics();
int ans = 0;
paper2.setColor(Color.darkGray);
paper2.fillRect(290, 290, 227, 227);
for (int r = 0; r < 10; r++) {
ans = random.nextInt(6) + 1;
paper2.setColor(Color.white);
paper2.fillRect(303, 303, 201, 201);
paper2.setColor(Color.black);
Font f = new Font("Dialog", Font.PLAIN, 200);
paper2.setFont(f);
paper2.drawString(Integer.toString(ans), 350, 480);
try {
Thread.sleep(100);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
panel.getGraphics().drawImage(roll, 0, 0, this);
}
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
update();
//grid[pn[playerTurn].getxPos()][pn[playerTurn].getyPos()] = grid[pn[playerTurn].getxPos()][pn[playerTurn].getyPos()] - 100 * playerTurn;
//pn[playerTurn].move();
render();
//return ans;
}
public void turn(int p) {
BufferedImage turn = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper3 = turn.getGraphics();
paper3.setColor(Color.darkGray);
paper3.fillRect(90, 290, 629, 227);
paper3.setColor(Color.white);
paper3.fillRect(103, 303, 603, 201);
paper3.setColor(Color.black);
Font f = new Font("Dialog", Font.PLAIN, 150);
paper3.setFont(f);
paper3.drawString("Player " + Integer.toString(p + 1), 125, 455);
panel.getGraphics().drawImage(turn, 0, 0, this);
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
render();
}
/*public void askPlayers() {
Object[] possibilities = {"2", "3", "4"};
String s = (String)JOptionPane.showInputDialog(
panel,
"Please specify the number of players:",
"Number of players",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"2"
);
if ((s != null) && (s.length() > 0)) {
players = Integer.parseInt(s);
if (players == 2) {
grid[7][0] = 300;
}
if (players == 3) {
grid[7][0] = 600;
}
if (players == 4) {
grid[7][0] = 1000;
}
pn = new Player[players];
}
else {
askPlayers();
}
}*/
public void update() {
//grid[0][7] = grid[0][7] + 100 * (playerTurn + 1);
grid[pn[playerTurn + 1].getxPos()][pn[playerTurn + 1].getyPos()] = grid[pn[playerTurn + 1].getxPos()][pn[playerTurn + 1].getyPos()] + 100 * (playerTurn + 1);
playerTurn = (playerTurn + 1) % (players) +1;
}
/*public void move() {
if (grid[pn[playerTurn].getyPos()][pn[playerTurn].getxPos()]%10 == 0){
yMove = roll();
}
if (grid[pn[playerTurn].getyPos()][pn[playerTurn].getxPos()]%10 == 2){
yMove = roll();
}
if (grid[pn[playerTurn].getyPos()][pn[playerTurn].getxPos()]%10 == 1){
xMove = roll();
}
if (grid[pn[playerTurn].getyPos()][pn[playerTurn].getxPos()]%10 == 3){
xMove = roll();
}
//occupied();
pn[playerTurn].setxPos(pn[playerTurn].getxPos() + xMove);
pn[playerTurn].setyPos(pn[playerTurn].getyPos() + yMove);
System.out.println(pn[playerTurn].getxPos());
update();
}*/
/*
public void occupied() {
if (xPos + xMove > 7){
xMove = 7;
}
if (xPos + xMove < 0){
xMove = 0;
}
if (yPos + yMove > 7){
yMove = 7;
}
if (yPos + yMove < 0){
yMove = 0;
}
if (GUI.grid[yPos + yMove][xPos + xMove] >= 100){
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 0){
yMove--;
}
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 2){
yMove++;
}
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 1){
xMove++;
}
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 3){
xMove--;
}
occupied();
}
}*/
}
package com.company;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class GUI extends JFrame
{
private JButton start;
private JButton roll;
static int[][] grid = new int[][]{
{2, 1, 1, 1, 1, 1, 2, 4},
{1, 1, 0, 2, 0, 3, 3, 3},
{1, 1, 1, 11, 0, 0, 3, 3},
{1, 1, 0, 1, 0, 0, 13, 3},
{11, 1, 0, 1, 0, 0, 3, 3},
{1, 1, 0, 3, 3, 3, 3, 3},
{1, 1, 0, 0, 10, 0, 3, 3},
{0, 0, 0, 0, 0, 0, 0, 0},
};
private JPanel panel;
private Random random;
static int players;
int playerTurn = 1;
static int ans;
private static Player[] pn = new Player[players];
public void createGUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container window = getContentPane();
window.setLayout(new FlowLayout());
random = new Random();
panel = new JPanel();
panel.setPreferredSize(new Dimension(809, 809));
panel.setBackground(Color.lightGray);
window.add(panel);
start = new JButton("Start");
window.add(start);
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
askPlayers();
start.setVisible(false);
roll.setVisible(true);
render();
turn(playerTurn);
}
});
roll = new JButton("Roll");
window.add(roll);
roll.setVisible(false);
roll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
roll();
render();
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
turn(playerTurn);
update();
}
});
}
public void render(){
BufferedImage board = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper = board.getGraphics();
paper.setColor(Color.lightGray);
paper.fillRect(0, 0, 809, 809);
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
paper.setColor(Color.white);
paper.fillRect(1 + 101 * i, 1 + 101 * j, 100, 100);
if (grid[j][i]/10%10 == 1) {
paper.setColor(Color.yellow);
paper.fillRect(1 + 101 * i, 1 + 101 * j, 100, 100);
}
if (grid[j][i]/100 == 1) {
paper.setColor(Color.red);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}if (grid[j][i]/100 == 2) {
paper.setColor(Color.green);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
if (grid[j][i]/100 == 3) {
paper.setColor(Color.blue);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
if (grid[j][i]/100 == 4) {
paper.setColor(Color.orange);
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
paper.setColor(Color.black);
if (grid[j][i]%10 == 0) {
paper.fillRect(46 + 101 * i,11 + 101 * j, 10, 80);
paper.fillRect(36 + 101 * i,21 + 101 * j, 30, 10);
paper.fillRect(26 + 101 * i,31 + 101 * j, 50, 10);
}
if (grid[j][i]%10 == 1) {
paper.fillRect(11 + 101 * i,46 + 101 * j, 80, 10);
paper.fillRect(71 + 101 * i,36 + 101 * j, 10, 30);
paper.fillRect(61 + 101 * i,26 + 101 * j, 10, 50);
}
if (grid[j][i]%10 == 2) {
paper.fillRect(46 + 101 * i,11 + 101 * j, 10, 80);
paper.fillRect(36 + 101 * i,71 + 101 * j, 30, 10);
paper.fillRect(26 + 101 * i,61 + 101 * j, 50, 10);
}
if (grid[j][i]%10 == 3) {
paper.fillRect(11 + 101 * i,46 + 101 * j, 80, 10);
paper.fillRect(21 + 101 * i, 36 + 101 * j, 10, 30);
paper.fillRect(31 + 101 * i,26 + 101 * j, 10, 50);
}
boolean white = false;
if (grid[j][i]%10 == 4) {
for (int x = 0; x < 10; x++) {
if (white) {
paper.setColor(Color.white);
white = false;
}
else {
paper.setColor(Color.black);
white = true;
}
for (int y = 0; y < 10; y++) {
if (white) {
paper.setColor(Color.white);
white = false;
}
else {
paper.setColor(Color.black);
white = true;
}
paper.fillRect(1 + 101 * i + x * 10, 1 + 101 * j + y * 10, 10, 10);
}
}
}
}
}
panel.getGraphics().drawImage(board, 0, 0, this);
}
public void roll() {
BufferedImage roll = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper = roll.getGraphics();
paper.setColor(Color.darkGray);
paper.fillRect(290, 290, 227, 227);
for (int r = 0; r < 10; r++) {
ans = random.nextInt(6) + 1;
paper.setColor(Color.white);
paper.fillRect(303, 303, 201, 201);
paper.setColor(Color.black);
Font f = new Font("Dialog", Font.PLAIN, 200);
paper.setFont(f);
paper.drawString(Integer.toString(ans), 350, 480);
try {
Thread.sleep(100);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
panel.getGraphics().drawImage(roll, 0, 0, this);
}
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
render();
}
public void turn(int p) {
BufferedImage turn = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper = turn.getGraphics();
paper.setColor(Color.darkGray);
paper.fillRect(90, 290, 629, 227);
paper.setColor(Color.white);
paper.fillRect(103, 303, 603, 201);
paper.setColor(Color.black);
Font f = new Font("Dialog", Font.PLAIN, 150);
paper.setFont(f);
paper.drawString("Player " + Integer.toString(p), 125, 455);
panel.getGraphics().drawImage(turn, 0, 0, this);
try {
Thread.sleep(2000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
playerTurn = playerTurn % (players) +1;
render();
pn[playerTurn].move();
render();
}
public void askPlayers() {
Object[] possibilities = {"2", "3", "4"};
String s = (String)JOptionPane.showInputDialog(
panel,
"Please specify the number of players:",
"Number of players",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
"2"
);
if ((s != null) && (s.length() > 0)) {
players = Integer.parseInt(s);
grid[7][0] = 100 * players;
}
else {
askPlayers();
}
}
public void update() {
grid[pn[playerTurn].getxPos()][pn[playerTurn].getyPos()] = grid[pn[playerTurn].getxPos()][pn[playerTurn].getyPos()] + 100;
}
}
package com.company;
public class Main {
public static void main(String[] args) {
GUI frame = new GUI();
frame.setSize(880, 910);
frame.createGUI();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class Player {
int xPos = 0;
int yPos = 7;
int xMove;
int yMove;
/*
public void move() {
if (GUI.grid[yPos][xPos]%10 == 0){
yMove = -GUI.ans;
}
if (GUI.grid[yPos][xPos]%10 == 2){
yMove = GUI.ans;
}
if (GUI.grid[yPos][xPos]%10 == 1){
xMove = GUI.ans;
}
if (GUI.grid[yPos][xPos]%10 == 3){
xMove = -GUI.ans;
}
//occupied();
xPos = xPos + xMove;
yPos = yPos + yMove;
}
public void occupied() {
if (xPos + xMove > 7){
xMove = 7;
}
if (xPos + xMove < 0){
xMove = 0;
}
if (yPos + yMove > 7){
yMove = 7;
}
if (yPos + yMove < 0){
yMove = 0;
}
if (GUI.grid[yPos + yMove][xPos + xMove] >= 100){
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 0){
yMove--;
}
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 2){
yMove++;
}
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 1){
xMove++;
}
if (GUI.grid[yPos + yMove][xPos + xMove]%10 == 3){
xMove--;
}
occupied();
}
}*/
public int getxPos() {
return xPos;
}
public int getyPos() {
return yPos;
}
public void setxPos(int x) {
xPos = x;
}
public void setyPos(int y) {
yPos = y;
}
public void draw() {
BufferedImage ship = new BufferedImage(809, 809, BufferedImage.TYPE_INT_ARGB);
Graphics paper = ship.getGraphics();
if (GUI.grid[yPos][xPos]/100 == 1) {
paper.setColor(Color.red);
}
if (GUI.grid[yPos][xPos]/100 == 2) {
paper.setColor(Color.green);
}
if (GUI.grid[yPos][xPos]/100 == 3) {
paper.setColor(Color.blue);
}
if (GUI.grid[yPos][xPos]/100 == 4) {
paper.setColor(Color.orange);
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
paper.fillRect(11 + 101 * i, 11 + 101 * j, 80, 80);
}
}
}
}
package com.company;
public class Player {
int xPos = 0;
int yPos = 7;
int xMove;
int yMove;
public void move() {
GUI.grid[xPos][yPos] = GUI.grid[xPos][yPos] - 100;
if (GUI.grid[xPos][yPos]%10 == 0){
yMove = -GUI.ans;
}
if (GUI.grid[xPos][yPos]%10 == 2){
yMove = GUI.ans;
}
if (GUI.grid[xPos][yPos]%10 == 1){
xMove = GUI.ans;
}
if (GUI.grid[xPos][yPos]%10 == 3){
xMove = -GUI.ans;
}
occupied();
xPos = xPos + xMove;
yPos = yPos + yMove;
}
public void occupied() {
if (xPos + xMove > 7){
xMove = 7;
}
if (xPos + xMove < 0){
xMove = 0;
}
if (yPos + yMove > 7){
yMove = 7;
}
if (yPos + yMove < 0){
yMove = 0;
}
if (GUI.grid[xPos + xMove][yPos + yMove] >= 100){
if (GUI.grid[xPos + xMove][yPos + yMove]%10 == 0){
yMove--;
}
if (GUI.grid[xPos + xMove][yPos + yMove]%10 == 2){
yMove++;
}
if (GUI.grid[xPos + xMove][yPos + yMove]%10 == 1){
xMove++;
}
if (GUI.grid[xPos + xMove][yPos + yMove]%10 == 3){
xMove--;
}
occupied();
}
}
public int getxPos() {
return xPos;
}
public int getyPos() {
return yPos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment