SlideShare a Scribd company logo
1 of 32
Download to read offline
λ‹΄λ‹Ήκ΅μˆ˜: λ°•λ™κ·œ
ν•™κ³Ό: 정보톡신곡학과
ν•™λ²ˆ: 20143091,20142325
이름: μ•ˆλ³‘κ±΄,ν™©μ°½μœ€
β—‡ λΉ„μ£Όμ–Ό ν”„λ‘œκ·Έλž˜λ°
기말 μ΅œμ’…λ³΄κ³ μ„œ
DATE :2019.12.17
1. κΈ°νšλ™κΈ°
λ§Žμ€ 학생듀이 β€™ν•™μ°½μ‹œμ ˆμ— 무슨 κ²Œμž„μ„ 많이 ν•΄λ΄€μ„κΉŒβ€˜ 생각을 해보닀가 쀑고등학
ꡐ μ‰¬λŠ” μ‹œκ°„μ— μΉœκ΅¬λ“€κ³Ό λ…ΈνŠΈμ— νŒμ„ κ·Έλ €μ„œ 동그라미와 Xν‘œλ‘œ 였λͺ©μ„ ν•˜λ˜ λ•Œκ°€ 생
κ°λ‚˜μ„œ 처음 κΈ°νšμ„ μ‹œμž‘ν•˜κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
였λͺ©μ΄λΌλŠ” κ²Œμž„μ€ λˆ„κ΅¬λ‚˜ ν•œ λ²ˆμ―€μ€ 해봀을 κ²Œμž„μ΄λΌκ³  생각해 μžλ°”λ‘œ κ΅¬ν˜„μ„ 해보
κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€.
2. ν”„λ‘œκ·Έλž¨ μ„€λͺ…
총 5개의 클래슀이며 각각의 μ„€λͺ…은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
- Player.java : ν•œ λͺ…μ˜ ν”Œλ ˆμ΄μ–΄λ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ 클래슀
- Board.java : ν•˜λ‚˜μ˜ κ²Œμž„μ— μ‚¬μš©λ˜λŠ” ν•˜λ‚˜μ˜ λ³΄λ“œνŒμ„ λ‚˜νƒ€λ‚΄λŠ” 클래슀
- Game.java : ν•œ 판의 κ²Œμž„μ„ μ§„ν–‰ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” 클래슀
- Main.java : 전체적인 ν”„λ‘œκ·Έλž¨μ˜ ν‹€κ³Ό κ²Œμž„ 진행을 μœ„ν•œ 메인 클래슀
- Restart.java : ν•œ κ²Œμž„μ΄ 끝났을 λ•Œ κ²Œμž„μ„ μž¬μ‹œμž‘ν•˜κΈ° μœ„ν•œ 클래슀
이미지 파일
- black.png : 검은색 돌 이미지
- white.png : ν•˜μ–€μƒ‰ 돌 이미지
- board.png : λ°”λ‘‘νŒ
기본적으둜 더블 버퍼링 기법을 μ‚¬μš©ν•˜μ—¬ μ‹€μ‹œκ°„μœΌλ‘œ 이미지λ₯Ό μ²˜λ¦¬ν•œλ‹€.
이미지 ν΄λ”μ—λŠ” λ°”λ‘‘λŒ 이미지, λ°”λ‘‘νŒ 이미지λ₯Ό λ„£μ—ˆλ‹€.
λ³΄λ“œνŒμ—λŠ” λŒμ„ ν•œ 번 λ‘˜ λ•Œλ§ˆλ‹€ draw()ν•¨μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ λ°”λ‘‘νŒμ— 그리게 λœλ‹€.
3. μ‹€ν–‰ν™”λ©΄
[μ‹€ν–‰ 첫 ν™”λ©΄]
[μ‹€ν–‰ μ‹œ ν”Œλ ˆμ΄μ–΄ μ°¨λ‘€ μ„€λͺ…]
[λ‘˜ 수 μ—†λŠ” 곳에 λ‘μ—ˆμ„ λ•Œ]
[μŠΉλ¦¬μžνŒλ³„]
[μŠΉλ¦¬μžνŒλ³„ ν›„ 3초 ν›„ μž¬μ‹œμž‘]
[μ—¬λŸ¬ μ°¨λ‘€ κ²Œμž„ 진행]
4. μ½”λ“œ
<board>
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Board{
// λ³΄λ“œνŒ κΈ°λ³Έ λ³€μˆ˜ μ„ μ–Έ
private final int WIDTH = 480, HEIGHT = 510;
private final int BLACK = 1, WHITE = 2;
private int boardXSize = 15, boardYSize = 15;
private int[][] boardMap = new int[boardXSize][boardYSize];
private Player playerOne, playerTwo;
private Player Winner;
private Image boardImage;
private Image blackImage;
private Image whiteImage;
// λ³΄λ“œνŒ μƒμ„±μž μ •μ˜
public Board()
{
boardImage = new
ImageIcon(getClass().getResource("/image/board.png")).getImage();
blackImage = new
ImageIcon(getClass().getResource("/image/black.png")).getImage();
whiteImage = new
ImageIcon(getClass().getResource("/image/white.png")).getImage();
for(int i = 0; i < boardXSize; i++)
{
for(int j = 0; j < boardYSize; j++)
{
// λ³΄λ“œνŒ ν–‰λ ¬ μ΄ˆκΈ°ν™”
boardMap[i][j] = 0;
}
}
}
// λ³΄λ“œνŒμ„ κ·Έλ¦¬λŠ” ν•¨μˆ˜
public void draw(Graphics g) {
g.drawImage(boardImage, 0, 30, null);
for(int i = 1; i <= boardXSize; i++)
g.drawLine(30 * i, 60, 30 * i, 480);
for(int i = 1; i <= boardYSize; i++)
g.drawLine(30, 30 + 30 * i, 450, 30 + 30 * i);
for(int i = 1; i <= boardXSize; i++)
{
for(int j = 1; j <= boardYSize; j++)
{
if(boardMap[i - 1][j - 1] != 0)
{
if(boardMap[i - 1][j - 1] == BLACK)
g.drawImage(blackImage, 30 * i - 15, 15
+ 30 * j, null);
if(boardMap[i - 1][j - 1] == WHITE)
g.drawImage(whiteImage, 30 * i - 15, 15
+ 30 * j, null);
}
}
}
}
// λ³΄λ“œνŒμ— λŒμ„ λ†“λŠ” ν•¨μˆ˜
public boolean put(int x, int y, Player player)
{
if(boardMap[x][y] != 0)
return false;
boardMap[x][y] = player.getPlayerColor();
return true;
}
// ν˜„μž¬ λ³΄λ“œνŒμ„ 좜λ ₯
public void show()
{
for(int i = 0; i < boardXSize; i++)
{
for(int j = 0; j < boardYSize; j++)
{
System.out.print(boardMap[i][j] + " ");
}
System.out.print("n");
}
}
// 승리자λ₯Ό λ°˜ν™˜
public Player getWinner()
{
return Winner;
}
// ν”Œλ ˆμ΄μ–΄ 승리 νŒμ •
public boolean win(int x, int y, Player player)
{
if(win1(x, y)||win2(x, y)||win3(x, y)||win4(x, y)||win5(x, y)||
win6(x, y)||win7(x, y)||win8(x, y)||win9(x, y)||win10(x,
y)||
win11(x, y)||win12(x, y)||win13(x, y)||win14(x,
y)||win15(x, y)||
win16(x, y)||win17(x, y)||win18(x, y)||win19(x,
y)||win20(x, y))
{
Winner = player;
return true;
}
return false;
}
/*------------- ν•œ 번 λŒμ„ 놓을 λ•Œλ§ˆλ‹€ 총 20가지 승리 κ°€λŠ₯ 경우의
μˆ˜κ°€ 쑴재 -------------*/
public boolean win1(int x, int y)
{
try
{
for(int i = y; i < y + 5; i++)
{
if(boardMap[x][i] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win2(int x, int y)
{
try
{
for(int i = x, j = y; i < x + 5; i++, j--)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win3(int x, int y)
{
try
{
for(int i = x; i < x + 5; i++)
{
if(boardMap[i][y] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win4(int x, int y)
{
try
{
for(int i = x, j = y; i < x + 5; i++, j++)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win5(int x, int y)
{
try
{
for(int i = y; i > y - 5; i--)
{
if(boardMap[x][i] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win6(int x, int y)
{
try
{
for(int i = x, j = y; i > x - 5; i--, j++)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win7(int x, int y)
{
try
{
for(int i = x; i > x - 5; i--)
{
if(boardMap[i][y] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win8(int x, int y)
{
try
{
for(int i = x, j = y; i > x - 5; i--, j--)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win9(int x, int y)
{
try
{
for(int i = y - 1; i < y + 4; i++)
{
if(boardMap[x][i] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win10(int x, int y)
{
try
{
for(int i = x - 1, j = y + 1; i < x + 4; i++, j--)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win11(int x, int y)
{
try
{
for(int i = x - 1; i < x + 4; i++)
{
if(boardMap[i][y] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win12(int x, int y)
{
try
{
for(int i = x - 1, j = y - 1; i < x + 4; i++, j++)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win13(int x, int y)
{
try
{
for(int i = y + 1; i > y - 4; i--)
{
if(boardMap[x][i] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win14(int x, int y)
{
try
{
for(int i = x + 1, j = y - 1; i > x - 4; i--, j++)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win15(int x, int y)
{
try
{
for(int i = x + 1; i > x - 4; i--)
{
if(boardMap[i][y] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win16(int x, int y)
{
try
{
for(int i = x + 1, j = y + 1; i > x - 4; i--, j--)
{
if(boardMap[i][j] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win17(int x, int y)
{
try
{
for(int i = y - 2; i < y + 3; i++)
{
if(boardMap[x][i] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win18(int x, int y)
{
try
{
for(int i = x - 2; i < x + 3; i++)
{
if(boardMap[i][y] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win19(int x, int y)
{
try
{
for(int i = y + 2; i > y - 3; i--)
{
if(boardMap[x][i] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
public boolean win20(int x, int y)
{
try
{
for(int i = x + 2; i > x - 3; i--)
{
if(boardMap[i][y] != boardMap[x][y])
{
return false;
}
}
}
catch(ArrayIndexOutOfBoundsException e)
{
return false;
}
return true;
}
}
<Game>
import java.awt.Graphics;
import java.util.Random;
import java.util.Scanner;
public class Game{
Board board = new Board();
private static int BLACK = 1;
private static int WHITE = 2;
public static Player attacker;
Player playerOne = new Player("μ•ˆλ³‘κ±΄", "μ•ˆλ³‘κ±΄μž…λ‹ˆλ‹€.");
Player playerTwo = new Player("ν™©μ°½μœ€", "ν™©μ°½μœ€μž…λ‹ˆλ‹€.");
public boolean ready = true;
public Game()
{
playerSetting(playerOne, playerTwo);
if(playerOne.getPlayerColor() == BLACK)
attacker = playerOne;
else
attacker = playerTwo;
}
public boolean attack(int x, int y)
{
Main.textArea.requestFocus();
if(!board.put(x, y, attacker))
{
Main.textArea.append("놓을 수 μ—†λŠ” μžλ¦¬μž…λ‹ˆλ‹€.n");
Main.textArea.append("λ‘˜ μœ„μΉ˜λ₯Ό λ‹€μ‹œ μ„ νƒν•˜μ„Έμš”n");
return false;
}
if(board.win(x, y, attacker))
{
Main.textArea.append("μŠΉλ¦¬μžλŠ” " +
board.getWinner().getPlayerName() + "μž…λ‹ˆλ‹€.n");
ready = false;
return true;
}
else
{
if(playerOne == attacker)
attacker = playerTwo;
else
attacker = playerOne;
Main.textArea.append(attacker.getPlayerName() + " ν”Œλ ˆμ΄μ–΄κ°€
λ‘˜ μ°¨λ‘€μž…λ‹ˆλ‹€.n");
}
return false;
}
public void draw(Graphics g)
{
board.draw(g);
}
// ν”Œλ ˆμ΄μ–΄ μˆœμ„œ κ²°μ •
public static void playerSetting(Player playerOne, Player playerTwo)
{
Random random = new Random();
if(random.nextBoolean() == true)
{
playerOne.setPlayerColor(BLACK);
playerTwo.setPlayerColor(WHITE);
}
else
{
playerOne.setPlayerColor(WHITE);
playerTwo.setPlayerColor(BLACK);
}
}
}
<Main>
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main extends JFrame{
private final int BLACK = 1, WHITE = 2;
private int boardXSize = 15, boardYSize = 15;
private final int WIDTH = 480, HEIGHT = 510;
private static Game game = new Game();
public static Main main;
Image dbImage;
Graphics dbG;
public static JTextArea textArea = new JTextArea(10, 20);
public static JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
public Main() {
setSize(WIDTH + 500, HEIGHT);
setResizable(false);
setVisible(true);
setTitle("였λͺ© κ²Œμž„");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(new MouseHandler());
setLocationRelativeTo(null);
setLayout(null);
scrollPane.setBounds(WIDTH + 20, 25, 455, 430);
add(scrollPane);
scrollPane.setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
dbImage = createImage(WIDTH, HEIGHT);
dbG = dbImage.getGraphics();
draw(dbG);
g.drawImage(dbImage, 0, 0, null);
}
public void draw(Graphics g) {
game.draw(g);
}
public static void main(String[] args) {
main = new Main();
textArea.append(game.attacker.getPlayerName() + " ν”Œλ ˆμ΄μ–΄κ°€ λ‘˜
μ°¨λ‘€μž…λ‹ˆλ‹€.n");
}
public static void restart() {
game = new Game();
textArea.append("κ²Œμž„μ΄ μž¬μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.n");
main.repaint();
}
public class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
repaint();
int mx = e.getX();
int my = e.getY();
if(game.ready)
{
for(int i = 0; i < boardXSize; i++)
{
for(int j = 0; j < boardYSize; j++)
{
if(mx > 30 * i + 15 && mx < 30 * i + 45
&& my > 30 * j + 45 && my < 30 * j + 75)
{
if(game.attack(i, j))
{
textArea.append("3초 후에
κ²Œμž„μ΄ μž¬μ‹œμž‘λ©λ‹ˆλ‹€.n");
Restart restart = new
Restart();
Thread restartThread = new
Thread(restart);
restartThread.start();
}
}
}
}
}
}
}
}
<Player>
public class Player {
// ν”Œλ ˆμ΄μ–΄ κΈ°λ³Έ λ³€μˆ˜ μ„ μ–Έ
private String playerName;
private String playerInfo;
private int playerColor;
// ν”Œλ ˆμ΄μ–΄ μƒμ„±μž μ •μ˜
public Player(String playerName, String playerInfo) {
this.playerName = playerName;
this.playerInfo = playerInfo;
}
// ν”Œλ ˆμ΄μ–΄μ˜ 돌 색깔을 λ°˜ν™˜
int getPlayerColor()
{
return playerColor;
}
// ν”Œλ ˆμ΄μ–΄μ˜ 돌 색깔을 μ„€μ •
void setPlayerColor(int playerColor)
{
this.playerColor = playerColor;
}
// ν”Œλ ˆμ΄μ–΄ 이름을 λ°˜ν™˜
public String getPlayerName() {
return playerName;
}
// ν”Œλ ˆμ΄μ–΄ 이름을 μ„€μ •
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
// ν”Œλ ˆμ΄μ–΄ 정보λ₯Ό λ°˜ν™˜
public String getPlayerInfo() {
return playerInfo;
}
// ν”Œλ ˆμ΄μ–΄ 정보λ₯Ό μ„€μ •
public void setPlayerInfo(String playerInfo) {
this.playerInfo = playerInfo;
}
}
<Restart>
public class Restart implements Runnable{
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Main.restart();
}

More Related Content

More from Hwangcy

Image game
Image gameImage game
Image gameHwangcy
Β 
Java Omok Project
Java Omok ProjectJava Omok Project
Java Omok ProjectHwangcy
Β 
Ppt
PptPpt
PptHwangcy
Β 
Image game
Image gameImage game
Image gameHwangcy
Β 
Java
JavaJava
JavaHwangcy
Β 
Image
ImageImage
ImageHwangcy
Β 
Rhr
RhrRhr
RhrHwangcy
Β 
Omok game
Omok gameOmok game
Omok gameHwangcy
Β 
Tetris
TetrisTetris
TetrisHwangcy
Β 
Rhr
RhrRhr
RhrHwangcy
Β 
Java project
Java projectJava project
Java projectHwangcy
Β 
μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨
μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨
μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨Hwangcy
Β 
μžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ 기말과제
μžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ κΈ°λ§κ³Όμ œμžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ 기말과제
μžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ 기말과제Hwangcy
Β 

More from Hwangcy (13)

Image game
Image gameImage game
Image game
Β 
Java Omok Project
Java Omok ProjectJava Omok Project
Java Omok Project
Β 
Ppt
PptPpt
Ppt
Β 
Image game
Image gameImage game
Image game
Β 
Java
JavaJava
Java
Β 
Image
ImageImage
Image
Β 
Rhr
RhrRhr
Rhr
Β 
Omok game
Omok gameOmok game
Omok game
Β 
Tetris
TetrisTetris
Tetris
Β 
Rhr
RhrRhr
Rhr
Β 
Java project
Java projectJava project
Java project
Β 
μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨
μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨
μžλ°” 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨
Β 
μžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ 기말과제
μžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ κΈ°λ§κ³Όμ œμžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ 기말과제
μžλ°”λ₯Ό μ΄μš©ν•œ 닀쀑 μ±„νŒ… ν”„λ‘œκ·Έλž¨ 기말과제
Β 

Java Final Report

  • 1. λ‹΄λ‹Ήκ΅μˆ˜: λ°•λ™κ·œ ν•™κ³Ό: 정보톡신곡학과 ν•™λ²ˆ: 20143091,20142325 이름: μ•ˆλ³‘κ±΄,ν™©μ°½μœ€ β—‡ λΉ„μ£Όμ–Ό ν”„λ‘œκ·Έλž˜λ° 기말 μ΅œμ’…λ³΄κ³ μ„œ DATE :2019.12.17
  • 2. 1. κΈ°νšλ™κΈ° λ§Žμ€ 학생듀이 β€™ν•™μ°½μ‹œμ ˆμ— 무슨 κ²Œμž„μ„ 많이 ν•΄λ΄€μ„κΉŒβ€˜ 생각을 해보닀가 쀑고등학 ꡐ μ‰¬λŠ” μ‹œκ°„μ— μΉœκ΅¬λ“€κ³Ό λ…ΈνŠΈμ— νŒμ„ κ·Έλ €μ„œ 동그라미와 Xν‘œλ‘œ 였λͺ©μ„ ν•˜λ˜ λ•Œκ°€ 생 κ°λ‚˜μ„œ 처음 κΈ°νšμ„ μ‹œμž‘ν•˜κ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€. 였λͺ©μ΄λΌλŠ” κ²Œμž„μ€ λˆ„κ΅¬λ‚˜ ν•œ λ²ˆμ―€μ€ 해봀을 κ²Œμž„μ΄λΌκ³  생각해 μžλ°”λ‘œ κ΅¬ν˜„μ„ 해보 κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€. 2. ν”„λ‘œκ·Έλž¨ μ„€λͺ… 총 5개의 클래슀이며 각각의 μ„€λͺ…은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. - Player.java : ν•œ λͺ…μ˜ ν”Œλ ˆμ΄μ–΄λ₯Ό μ„€μ •ν•˜κΈ° μœ„ν•œ 클래슀 - Board.java : ν•˜λ‚˜μ˜ κ²Œμž„μ— μ‚¬μš©λ˜λŠ” ν•˜λ‚˜μ˜ λ³΄λ“œνŒμ„ λ‚˜νƒ€λ‚΄λŠ” 클래슀 - Game.java : ν•œ 판의 κ²Œμž„μ„ μ§„ν–‰ν•˜κΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” 클래슀 - Main.java : 전체적인 ν”„λ‘œκ·Έλž¨μ˜ ν‹€κ³Ό κ²Œμž„ 진행을 μœ„ν•œ 메인 클래슀 - Restart.java : ν•œ κ²Œμž„μ΄ 끝났을 λ•Œ κ²Œμž„μ„ μž¬μ‹œμž‘ν•˜κΈ° μœ„ν•œ 클래슀 이미지 파일 - black.png : 검은색 돌 이미지 - white.png : ν•˜μ–€μƒ‰ 돌 이미지 - board.png : λ°”λ‘‘νŒ 기본적으둜 더블 버퍼링 기법을 μ‚¬μš©ν•˜μ—¬ μ‹€μ‹œκ°„μœΌλ‘œ 이미지λ₯Ό μ²˜λ¦¬ν•œλ‹€. 이미지 ν΄λ”μ—λŠ” λ°”λ‘‘λŒ 이미지, λ°”λ‘‘νŒ 이미지λ₯Ό λ„£μ—ˆλ‹€. λ³΄λ“œνŒμ—λŠ” λŒμ„ ν•œ 번 λ‘˜ λ•Œλ§ˆλ‹€ draw()ν•¨μˆ˜λ₯Ό μ΄μš©ν•˜μ—¬ λ°”λ‘‘νŒμ— 그리게 λœλ‹€. 3. μ‹€ν–‰ν™”λ©΄ [μ‹€ν–‰ 첫 ν™”λ©΄]
  • 3. [μ‹€ν–‰ μ‹œ ν”Œλ ˆμ΄μ–΄ μ°¨λ‘€ μ„€λͺ…] [λ‘˜ 수 μ—†λŠ” 곳에 λ‘μ—ˆμ„ λ•Œ] [μŠΉλ¦¬μžνŒλ³„]
  • 4. [μŠΉλ¦¬μžνŒλ³„ ν›„ 3초 ν›„ μž¬μ‹œμž‘] [μ—¬λŸ¬ μ°¨λ‘€ κ²Œμž„ 진행]
  • 5. 4. μ½”λ“œ <board> import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; public class Board{ // λ³΄λ“œνŒ κΈ°λ³Έ λ³€μˆ˜ μ„ μ–Έ private final int WIDTH = 480, HEIGHT = 510; private final int BLACK = 1, WHITE = 2; private int boardXSize = 15, boardYSize = 15; private int[][] boardMap = new int[boardXSize][boardYSize]; private Player playerOne, playerTwo; private Player Winner; private Image boardImage; private Image blackImage; private Image whiteImage; // λ³΄λ“œνŒ μƒμ„±μž μ •μ˜ public Board() { boardImage = new ImageIcon(getClass().getResource("/image/board.png")).getImage(); blackImage = new ImageIcon(getClass().getResource("/image/black.png")).getImage(); whiteImage = new ImageIcon(getClass().getResource("/image/white.png")).getImage(); for(int i = 0; i < boardXSize; i++)
  • 6. { for(int j = 0; j < boardYSize; j++) { // λ³΄λ“œνŒ ν–‰λ ¬ μ΄ˆκΈ°ν™” boardMap[i][j] = 0; } } } // λ³΄λ“œνŒμ„ κ·Έλ¦¬λŠ” ν•¨μˆ˜ public void draw(Graphics g) { g.drawImage(boardImage, 0, 30, null); for(int i = 1; i <= boardXSize; i++) g.drawLine(30 * i, 60, 30 * i, 480); for(int i = 1; i <= boardYSize; i++) g.drawLine(30, 30 + 30 * i, 450, 30 + 30 * i); for(int i = 1; i <= boardXSize; i++) { for(int j = 1; j <= boardYSize; j++) { if(boardMap[i - 1][j - 1] != 0) { if(boardMap[i - 1][j - 1] == BLACK) g.drawImage(blackImage, 30 * i - 15, 15 + 30 * j, null); if(boardMap[i - 1][j - 1] == WHITE) g.drawImage(whiteImage, 30 * i - 15, 15
  • 7. + 30 * j, null); } } } } // λ³΄λ“œνŒμ— λŒμ„ λ†“λŠ” ν•¨μˆ˜ public boolean put(int x, int y, Player player) { if(boardMap[x][y] != 0) return false; boardMap[x][y] = player.getPlayerColor(); return true; } // ν˜„μž¬ λ³΄λ“œνŒμ„ 좜λ ₯ public void show() { for(int i = 0; i < boardXSize; i++) { for(int j = 0; j < boardYSize; j++) { System.out.print(boardMap[i][j] + " "); } System.out.print("n"); }
  • 8. } // 승리자λ₯Ό λ°˜ν™˜ public Player getWinner() { return Winner; } // ν”Œλ ˆμ΄μ–΄ 승리 νŒμ • public boolean win(int x, int y, Player player) { if(win1(x, y)||win2(x, y)||win3(x, y)||win4(x, y)||win5(x, y)|| win6(x, y)||win7(x, y)||win8(x, y)||win9(x, y)||win10(x, y)|| win11(x, y)||win12(x, y)||win13(x, y)||win14(x, y)||win15(x, y)|| win16(x, y)||win17(x, y)||win18(x, y)||win19(x, y)||win20(x, y)) { Winner = player; return true; } return false; } /*------------- ν•œ 번 λŒμ„ 놓을 λ•Œλ§ˆλ‹€ 총 20가지 승리 κ°€λŠ₯ 경우의 μˆ˜κ°€ 쑴재 -------------*/
  • 9. public boolean win1(int x, int y) { try { for(int i = y; i < y + 5; i++) { if(boardMap[x][i] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win2(int x, int y) { try { for(int i = x, j = y; i < x + 5; i++, j--) { if(boardMap[i][j] != boardMap[x][y])
  • 10. { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win3(int x, int y) { try { for(int i = x; i < x + 5; i++) { if(boardMap[i][y] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) {
  • 11. return false; } return true; } public boolean win4(int x, int y) { try { for(int i = x, j = y; i < x + 5; i++, j++) { if(boardMap[i][j] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win5(int x, int y) {
  • 12. try { for(int i = y; i > y - 5; i--) { if(boardMap[x][i] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win6(int x, int y) { try { for(int i = x, j = y; i > x - 5; i--, j++) { if(boardMap[i][j] != boardMap[x][y]) { return false;
  • 13. } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win7(int x, int y) { try { for(int i = x; i > x - 5; i--) { if(boardMap[i][y] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; }
  • 14. return true; } public boolean win8(int x, int y) { try { for(int i = x, j = y; i > x - 5; i--, j--) { if(boardMap[i][j] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win9(int x, int y) { try {
  • 15. for(int i = y - 1; i < y + 4; i++) { if(boardMap[x][i] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win10(int x, int y) { try { for(int i = x - 1, j = y + 1; i < x + 4; i++, j--) { if(boardMap[i][j] != boardMap[x][y]) { return false; } }
  • 16. } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win11(int x, int y) { try { for(int i = x - 1; i < x + 4; i++) { if(boardMap[i][y] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; }
  • 17. public boolean win12(int x, int y) { try { for(int i = x - 1, j = y - 1; i < x + 4; i++, j++) { if(boardMap[i][j] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win13(int x, int y) { try { for(int i = y + 1; i > y - 4; i--) {
  • 18. if(boardMap[x][i] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win14(int x, int y) { try { for(int i = x + 1, j = y - 1; i > x - 4; i--, j++) { if(boardMap[i][j] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e)
  • 19. { return false; } return true; } public boolean win15(int x, int y) { try { for(int i = x + 1; i > x - 4; i--) { if(boardMap[i][y] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win16(int x, int y)
  • 20. { try { for(int i = x + 1, j = y + 1; i > x - 4; i--, j--) { if(boardMap[i][j] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win17(int x, int y) { try { for(int i = y - 2; i < y + 3; i++) { if(boardMap[x][i] != boardMap[x][y]) {
  • 21. return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win18(int x, int y) { try { for(int i = x - 2; i < x + 3; i++) { if(boardMap[i][y] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false;
  • 22. } return true; } public boolean win19(int x, int y) { try { for(int i = y + 2; i > y - 3; i--) { if(boardMap[x][i] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } public boolean win20(int x, int y) { try
  • 23. { for(int i = x + 2; i > x - 3; i--) { if(boardMap[i][y] != boardMap[x][y]) { return false; } } } catch(ArrayIndexOutOfBoundsException e) { return false; } return true; } } <Game> import java.awt.Graphics; import java.util.Random; import java.util.Scanner; public class Game{ Board board = new Board(); private static int BLACK = 1;
  • 24. private static int WHITE = 2; public static Player attacker; Player playerOne = new Player("μ•ˆλ³‘κ±΄", "μ•ˆλ³‘κ±΄μž…λ‹ˆλ‹€."); Player playerTwo = new Player("ν™©μ°½μœ€", "ν™©μ°½μœ€μž…λ‹ˆλ‹€."); public boolean ready = true; public Game() { playerSetting(playerOne, playerTwo); if(playerOne.getPlayerColor() == BLACK) attacker = playerOne; else attacker = playerTwo; } public boolean attack(int x, int y) { Main.textArea.requestFocus(); if(!board.put(x, y, attacker)) { Main.textArea.append("놓을 수 μ—†λŠ” μžλ¦¬μž…λ‹ˆλ‹€.n"); Main.textArea.append("λ‘˜ μœ„μΉ˜λ₯Ό λ‹€μ‹œ μ„ νƒν•˜μ„Έμš”n"); return false; } if(board.win(x, y, attacker))
  • 25. { Main.textArea.append("μŠΉλ¦¬μžλŠ” " + board.getWinner().getPlayerName() + "μž…λ‹ˆλ‹€.n"); ready = false; return true; } else { if(playerOne == attacker) attacker = playerTwo; else attacker = playerOne; Main.textArea.append(attacker.getPlayerName() + " ν”Œλ ˆμ΄μ–΄κ°€ λ‘˜ μ°¨λ‘€μž…λ‹ˆλ‹€.n"); } return false; } public void draw(Graphics g) { board.draw(g); } // ν”Œλ ˆμ΄μ–΄ μˆœμ„œ κ²°μ • public static void playerSetting(Player playerOne, Player playerTwo) {
  • 26. Random random = new Random(); if(random.nextBoolean() == true) { playerOne.setPlayerColor(BLACK); playerTwo.setPlayerColor(WHITE); } else { playerOne.setPlayerColor(WHITE); playerTwo.setPlayerColor(BLACK); } } } <Main> import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextArea;
  • 27. public class Main extends JFrame{ private final int BLACK = 1, WHITE = 2; private int boardXSize = 15, boardYSize = 15; private final int WIDTH = 480, HEIGHT = 510; private static Game game = new Game(); public static Main main; Image dbImage; Graphics dbG; public static JTextArea textArea = new JTextArea(10, 20); public static JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); public Main() { setSize(WIDTH + 500, HEIGHT); setResizable(false); setVisible(true); setTitle("였λͺ© κ²Œμž„"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); addMouseListener(new MouseHandler()); setLocationRelativeTo(null); setLayout(null);
  • 28. scrollPane.setBounds(WIDTH + 20, 25, 455, 430); add(scrollPane); scrollPane.setVisible(true); } public void paint(Graphics g) { super.paint(g); dbImage = createImage(WIDTH, HEIGHT); dbG = dbImage.getGraphics(); draw(dbG); g.drawImage(dbImage, 0, 0, null); } public void draw(Graphics g) { game.draw(g); } public static void main(String[] args) { main = new Main(); textArea.append(game.attacker.getPlayerName() + " ν”Œλ ˆμ΄μ–΄κ°€ λ‘˜ μ°¨λ‘€μž…λ‹ˆλ‹€.n"); } public static void restart() { game = new Game(); textArea.append("κ²Œμž„μ΄ μž¬μ‹œμž‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.n"); main.repaint();
  • 29. } public class MouseHandler extends MouseAdapter { @Override public void mousePressed(MouseEvent e) { repaint(); int mx = e.getX(); int my = e.getY(); if(game.ready) { for(int i = 0; i < boardXSize; i++) { for(int j = 0; j < boardYSize; j++) { if(mx > 30 * i + 15 && mx < 30 * i + 45 && my > 30 * j + 45 && my < 30 * j + 75) { if(game.attack(i, j)) { textArea.append("3초 후에 κ²Œμž„μ΄ μž¬μ‹œμž‘λ©λ‹ˆλ‹€.n"); Restart restart = new Restart(); Thread restartThread = new Thread(restart);
  • 30. restartThread.start(); } } } } } } } } <Player> public class Player { // ν”Œλ ˆμ΄μ–΄ κΈ°λ³Έ λ³€μˆ˜ μ„ μ–Έ private String playerName; private String playerInfo; private int playerColor; // ν”Œλ ˆμ΄μ–΄ μƒμ„±μž μ •μ˜ public Player(String playerName, String playerInfo) { this.playerName = playerName; this.playerInfo = playerInfo; } // ν”Œλ ˆμ΄μ–΄μ˜ 돌 색깔을 λ°˜ν™˜ int getPlayerColor()
  • 31. { return playerColor; } // ν”Œλ ˆμ΄μ–΄μ˜ 돌 색깔을 μ„€μ • void setPlayerColor(int playerColor) { this.playerColor = playerColor; } // ν”Œλ ˆμ΄μ–΄ 이름을 λ°˜ν™˜ public String getPlayerName() { return playerName; } // ν”Œλ ˆμ΄μ–΄ 이름을 μ„€μ • public void setPlayerName(String playerName) { this.playerName = playerName; } // ν”Œλ ˆμ΄μ–΄ 정보λ₯Ό λ°˜ν™˜ public String getPlayerInfo() { return playerInfo; } // ν”Œλ ˆμ΄μ–΄ 정보λ₯Ό μ„€μ •
  • 32. public void setPlayerInfo(String playerInfo) { this.playerInfo = playerInfo; } } <Restart> public class Restart implements Runnable{ @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } Main.restart(); }