import java.awt.Color;
public class Cell {
private Color color;
private boolean revealed;
public Cell(Color c) {
color = c;
}
public Color color() {
return color;
}
public boolean revealed() {
return revealed;
}
public void reveal() {
revealed = true;
}
public void hide() {
revealed = false;
}
public boolean isATwinWith(Cell c) {
return true; // true to be replaced with a proper expression
}
}
import java.awt.Color;
import java.util.Random;
public class GameBoard {
private Cell[][] square;
private int point;
public Cell[][] square() {
return square;
}
public int point() {
return point;
}
public void upgrade() {
point += 2;
}
public GameBoard() {
square = new Cell[4][4];
Color[] colors = {Color.BLUE, Color.CYAN, Color.PINK, Color.RED,
Color.GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE};
Cell[] twins = createTwinCells(colors);
makeSquare(shuffle(twins));
}
private void makeSquare(Cell[] cells) {
// to be filled...
}
private Cell[] createTwinCells(Color[] cs) {
// to be filled...
}
private Cell[] shuffle(Cell[] twins) {
int index;
Cell temp;
Random random = new Random();
for (int i = twins.length - 1; i > 0; i--) {
index = random.nextInt(i+1);
temp = twins[index];
twins[index] = twins[i];
twins[i] = temp;
}
return twins;
}
}
import javax.swing.JOptionPane;
public class Reader {
public int readInput() {
String input = JOptionPane.showInputDialog("번호를 선택하세요.");
int number = Integer.parseInt(input);
while (number < 1 || number > 16) {
input = JOptionPane.showInputDialog("번호를 선택하세요.");
number = Integer.parseInt(input);
}
return number;
}
}
import javax.swing.*;
import java.awt.*;
public class BoardWriter extends JPanel {
private GameBoard board;
private final int SIZE = 30;
public BoardWriter(GameBoard b) {
board = b;
JFrame f = new JFrame();
f.getContentPane().add(this);
f.setLocation(550,80);
f.setTitle("Matching Game");
f.setSize(SIZE*6, SIZE*7+28);
f.setVisible(true);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, SIZE*6, SIZE*7);
paintCellNumbers(g);
paintRevealedCells(g);
}
private void paintCellNumbers(Graphics g) {
int number = 1;
for (int i = 0; i < 4; i = i + 1)
for (int j = 0; j < 4; j = j + 1) {
int x = SIZE + (SIZE * j);
int y = SIZE + (SIZE * i);
g.setColor(Color.BLACK);
g.drawRect(x, y, SIZE, SIZE);
if (number < 10)
g.drawString(number + "", x+11, y+20);
else
g.drawString(number + "", x+7, y+20);
number += 1;
}
}
private void paintRevealedCells(Graphics g) {
Cell[][] square = board.square();
for (int i = 0; i < 4; i = i + 1)
for (int j = 0; j < 4; j = j + 1) {
Cell cell = square[i][j];
if (cell.revealed()) {
int x = SIZE + (SIZE * j);
int y = SIZE + (SIZE * i);
g.setColor(cell.color());
g.fillOval(x+3, y+3, SIZE-6, SIZE-6);
}
}
}
}
public class GameController {
private GameBoard board;
private BoardWriter writer;
private Reader reader;
public GameController(GameBoard b, BoardWriter w, Reader r) {
board = b;
writer = w;
reader = r;
}
public void play() {
Cell[][] square = board.square();
// to be filled...
}
/** delay - how_long millisecond 동안 실행 정지 */
private void delay(int how_long) {
try { Thread.sleep(how_long); }
catch (InterruptedException e) { }
}
}
public class MatchingGame {
public static void main(String[] args) {
GameBoard board = new GameBoard();
BoardWriter writer = new BoardWriter(board);
Reader reader = new Reader();
new GameController(board,writer,reader).play();
}
}
import java.awt.Color;
public class Cell {
private Color color;
private boolean revealed;
public Cell(Color c) {
color = c;
}
public Color color() {
return color;
}
public boolean revealed() {
return revealed;
}
public void reveal() {
revealed = true;
}
public void hide() {
revealed = false;
}
public boolean isATwinWith(Cell c) {
return color.equals(c.color());
}
}
import java.awt.Color;
import java.util.Random;
public class GameBoard {
private Cell[][] square;
private int point;
public Cell[][] square() {
return square;
}
public int point() {
return point;
}
public void upgrade() {
point += 2;
}
public GameBoard() {
square = new Cell[4][4];
Color[] colors = {Color.BLUE, Color.CYAN, Color.PINK, Color.RED,
Color.GRAY, Color.GREEN, Color.MAGENTA, Color.ORANGE};
Cell[] twins = createTwinCells(colors);
makeSquare(shuffle(twins));
}
private void makeSquare(Cell[] cells) {
int index = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
square[i][j] = cells[index];
index += 1;
}
}
private Cell[] createTwinCells(Color[] cs) {
Cell[] twins = new Cell[cs.length*2];
int i = 0;
for (int k = 0; k < cs.length; k++) {
twins[i] = new Cell(cs[k]);
i += 1;
twins[i] = new Cell(cs[k]);
i += 1;
}
return twins;
}
private Cell[] shuffle(Cell[] twins) {
int index;
Cell temp;
Random random = new Random();
for (int i = twins.length - 1; i > 0; i--) {
index = random.nextInt(i+1);
temp = twins[index];
twins[index] = twins[i];
twins[i] = temp;
}
return twins;
}
}
public class GameController {
private GameBoard board;
private BoardWriter writer;
private Reader reader;
public GameController(GameBoard b, BoardWriter w, Reader r) {
board = b;
writer = w;
reader = r;
}
public void play() {
Cell[][] square = board.square();
while (board.point() < 16) {
int fst = reader.readInput() - 1;
Cell cell1 = square[fst/4][fst%4];
cell1.reveal();
writer.repaint();
int snd = reader.readInput() - 1;
while (fst == snd) {
snd = reader.readInput() - 1;
}
Cell cell2 = square[snd/4][snd%4];
cell2.reveal();
writer.repaint();
delay(2000);
if (cell1.isATwinWith(cell2)) {
board.upgrade();
}
else {
cell1.hide();
cell2.hide();
}
writer.repaint();
}
}
/** delay - how_long millisecond 동안 실행 정지 */
private void delay(int how_long) {
try { Thread.sleep(how_long); }
catch (InterruptedException e) { }
}
}