# 4์ฅ Lab ๋์ ๋ฌธ์ ํ์ด
155p_Lab ๋์ ๋ฌธ์ )
package chap04;
import java.util.*;
public class Lab_Advanced_155p {
public static void main(String[] args) {
int STUDENTS = 1;
int total = 0;
Scanner scan = new Scanner(System.in);
System.out.println("ํ์ ์: ");
STUDENTS = scan.nextInt();
int[] scores = new int[STUDENTS];
for (int i = 0; i < scores.length; i++)
{
System.out.print("์ฑ์ ์ ์
๋ ฅํ์์ค: ");
scores[i] = scan.nextInt();
}
for (int i = 0; i < scores.length; i++)
{
total = total + scores[i];
}
System.out.println("ํ๊ท : " + (total/STUDENTS));
}
}
158p_Lab ๋์ ๋ฌธ์ )
package chap04;
public class Lab_Advanced_158p {
public static void main(String[] args) {
double[] numbers = { 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9 };
for ( int i = 0; i < numbers.length; i++)
{
if (numbers[i] == 0.9)
{
System.out.print(numbers[i]);
}
else
{
System.out.print(numbers[i] + ", ");
}
}
}
}
160p_Lab ๋์ ๋ฌธ์ )
package chap04;
public class Lab_Advanced_160p {
public static void main(String[] args) {
int s[] = { 12, 3, 14, 2, 11 };
int max_num;
max_num = s[0];
for(int i = 1; i < s.length; i++)
{
if(s[i] > max_num)
{
max_num = s[i];
}
}
System.out.print("์ต๋๊ฐ: " + max_num);
}
}
162p_Lab ๋์ ๋ฌธ์ )
package chap04;
import java.util.*;
public class Lab_Advanced_162p {
public static void main(String[] args) {
int s[] = { 1, 2, 3, 4, 5 };
int value, index = -1;
Scanner scan = new Scanner(System.in);
System.out.print("ํ์ํ ๊ฐ์ ์
๋ ฅํ์์ค: ");
value = scan.nextInt();
// ์ด์ง ํ์์ ์ํ ํ์ ๋ฒ์ ์ด๊ธฐํ
int low = 0; // ํ์ ๋ฒ์์ ๊ฐ์ฅ ์ผ์ชฝ ์ธ๋ฑ์ค
int high = s.length - 1; // ํ์ ๋ฒ์์ ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ์ธ๋ฑ์ค
while (low <= high)
{
int mid = (low + high) / 2; // ํ์ ๋ฒ์์ ์ค๊ฐ ์ธ๋ฑ์ค ๊ณ์ฐ
if (s[mid] == value)
{ // ์ฐพ๊ณ ์ ํ๋ ๊ฐ๊ณผ ์ผ์นํ๋ ๊ฒฝ์ฐ
index = mid; // ์ธ๋ฑ์ค ์ ์ฅ
break; // ํ์ ์ข
๋ฃ
}
else if (s[mid] < value)
{ // ์ค๊ฐ ๊ฐ์ด ์ฐพ๊ณ ์ ํ๋ ๊ฐ๋ณด๋ค ์์ ๊ฒฝ์ฐ
low = mid + 1; // ํ์ ๋ฒ์๋ฅผ ์ค๋ฅธ์ชฝ ๋ฐ์ชฝ์ผ๋ก ์ขํ
}
else
{ // ์ค๊ฐ ๊ฐ์ด ์ฐพ๊ณ ์ ํ๋ ๊ฐ๋ณด๋ค ํฐ ๊ฒฝ์ฐ
high = mid - 1; // ํ์ ๋ฒ์๋ฅผ ์ผ์ชฝ ๋ฐ์ชฝ์ผ๋ก ์ขํ
}
}
if (index >= 0) { // ์ฐพ์ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ
System.out.println(value + "๊ฐ์ " + index + "์์น์ ์๋ค.");
}
}
}
๋ ๋น ๋ฅธ ํ์์ ์ํด ์ด์ง ํ์(Binary Search) ๊ธฐ๋ฒ ์ฌ์ฉ
โป ์ด์ง ํ์(Binary Search) โป
ํ์ ๋ฒ์๋ฅผ ๋ฐ์ผ๋ก ๋๋์ด ์ฐพ๊ณ ์ ํ๋ ๊ฐ๊ณผ ๋น๊ตํ๋ฉด์ ํ์ ๋ฒ์๋ฅผ ์ขํ๋๊ฐ๋ ๋ฐฉ๋ฒ์ผ๋ก,
์ฃผ๋ก ๋ฐฐ์ด์ด ์ ๋ ฌ๋ ์ํ์์ ์ฌ์ฉ๋๋ค.
+ ์ด๋ฏธ์ง ์ถ์ฒ
177p_Lab ๋์ ๋ฌธ์ )
package chap04;
import java.util.Scanner;
import java.util.Random;
public class Lab_Advanced_177p {
public static void main(String[] args) {
char[][] board = new char[3][3];
int x;
int y;
Scanner scan = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
do {
for (int i = 0; i < 3; i++) {
System.out.println(" " + board[i][0] + "| " + board[i][1] + "| " + board[i][2]);
if (i != 2) {
System.out.println("---|---|---");
}
}
System.out.println("๋ค์ ์์ ์ขํ: ");
x = scan.nextInt();
y = scan.nextInt();
if (x < 0 || x > 2 || y < 0 || y > 2) {
System.out.println("์๋ชป๋ ์์น๋ฅผ ์ ํํ์
จ์ต๋๋ค.");
continue;
}
if (board[x][y] != ' ') {
System.out.println("์ด๋ฏธ ๋์ฌ์ง ์๋ฆฌ์
๋๋ค. ๋ค๋ฅธ ์๋ฆฌ๋ฅผ ์ ํํ์ธ์.");
continue;
} else {
board[x][y] = 'X';
}
// ๊ฒ์ํ์ด ๊ฐ๋ ์ฐผ๋์ง ํ์ธ
boolean isBoardFull = true;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
isBoardFull = false;
break;
}
}
}
// ๊ฒ์ํ์ด ๊ฐ๋ ์ฐผ์ผ๋ฉด ๊ฒ์ ์ข
๋ฃ
if (isBoardFull) {
System.out.println("๊ฒ์ ์ข
๋ฃ: ๋ ์ด์ ๋์ ๊ณต๊ฐ์ด ์์ต๋๋ค.");
break;
}
// ํ๋ ์ด์ด(X)๊ฐ ์น๋ฆฌํ๋์ง ํ์ธ
if (checkWin(board, 'X')) {
System.out.println("๊ฒ์ ์ข
๋ฃ: ํ๋ ์ด์ด (X) ์น๋ฆฌ!");
break;
}
// ์ปดํจํฐ์ ์ฐจ๋ก (O)
Random random = new Random();
int compX, compY;
// ์ปดํจํฐ๊ฐ ๋ค์ ์๋ก ์น๋ฆฌํ ์ ์๋์ง ํ์ธ
boolean isComputerWin = false;
for (compX = 0; compX < 3; compX++) {
for (compY = 0; compY < 3; compY++) {
if (board[compX][compY] == ' ') {
board[compX][compY] = 'O';
if (checkWin(board, 'O')) {
System.out.println("์ปดํจํฐ (O)๊ฐ ์ด๊ฒผ์ต๋๋ค!");
isComputerWin = true;
break;
}
board[compX][compY] = ' '; // ์๋ฅผ ๋๊ณ ๋ค์ ๋๋๋ฆผ
}
}
if (isComputerWin) {
break;
}
}
if (isComputerWin) {
scan.close();
return;
}
// ํ๋ ์ด์ด๊ฐ ๋ค์ ์๋ก ์น๋ฆฌํ ์ ์๋์ง ํ์ธํ๊ณ ๋ง๊ธฐ
boolean isPlayerWinBlocked = false;
for (compX = 0; compX < 3; compX++) {
for (compY = 0; compY < 3; compY++) {
if (board[compX][compY] == ' ') {
board[compX][compY] = 'X';
if (checkWin(board, 'X')) {
board[compX][compY] = 'O'; // ์น๋ฆฌํ๋ ์๋ฅผ ๋ง์
isPlayerWinBlocked = true;
break;
} else {
board[compX][compY] = ' '; // ์๋ฅผ ๋๊ณ ๋ค์ ๋๋๋ฆผ
}
}
}
if (isPlayerWinBlocked) {
break;
}
}
// ์น๋ฆฌํ ์ ์๋ ์๊ฐ ์๊ฑฐ๋ ๋ง์ ์ ์์ผ๋ฉด ๋ฌด์์๋ก ์๋ฅผ ๋์
if (!isComputerWin && !isPlayerWinBlocked) {
while (true) {
compX = random.nextInt(3);
compY = random.nextInt(3);
if (board[compX][compY] == ' ') {
board[compX][compY] = 'O';
break;
}
}
}
// ์ปดํจํฐ (O)๊ฐ ์น๋ฆฌํ๋์ง ํ์ธ
if (checkWin(board, 'O')) {
System.out.println("๊ฒ์ ์ข
๋ฃ: ์ปดํจํฐ (O) ์น๋ฆฌ!");
break;
}
} while (true);
scan.close();
}
// ํ๋ ์ด์ด (X ๋๋ O)๊ฐ ์น๋ฆฌํ๋์ง ํ์ธํ๋ ํจ์
public static boolean checkWin(char[][] board, char player) {
for (int i = 0; i < 3; i++) {
// ํ ํ์ธ
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
// ์ด ํ์ธ
if (board[0][i] == player && board[1][i] == player && board[2][i] == player) {
return true;
}
}
// ๋๊ฐ์ ํ์ธ
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return true;
}
return false;
}
}
++ ์ปดํจํฐ์ ์ ์ ํ์ ์ฌ์ฉํ ์๊ณ ๋ฆฌ์ฆ
A) ์ปดํจํฐ๊ฐ ๋ค์ ์๋ก ์น๋ฆฌํ ์ ์๋์ง ํ์ธํ๋ ์๊ณ ๋ฆฌ์ฆ
1๏ธโฃ isComputerWin ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํจํฐ์ ์น๋ฆฌ ์ฌ๋ถ ํ์ธ
2๏ธโฃ ์ด์ค ๋ฐ๋ณต๋ฌธ์ ํตํด board ๋ฐฐ์ด์ ๋น ๊ณต๊ฐ ํ์
3๏ธโฃ ๋น ๊ณต๊ฐ์ ์ปดํจํฐ('O')๋ฅผ ๋์ ํ checkWin ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ปดํจํฐ์ ์น๋ฆฌ ์ฌ๋ถ ํ์ธ
4๏ธโฃ ์ปดํจํฐ๊ฐ ์น๋ฆฌํ๋ฉด isComputerWin์ true๋ก ์ค์ ํ๊ณ ๋ฃจํ ํ์ถ
B) ์ปดํจํฐ๊ฐ ํ๋ ์ด์ด์ ์น๋ฆฌ๋ฅผ ๋ง๊ธฐ ์ํ ์๊ณ ๋ฆฌ์ฆ
1๏ธโฃ isPlayerWinBlocked ๋ถ๋ฆฌ์ธ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ ์ด์ด์ ์น๋ฆฌ๋ฅผ ๋ง์ ์ ์๋์ง ํ์ธ
2๏ธโฃ ๋ค์ ์ด์ค ๋ฐ๋ณต๋ฌธ์ ํตํด board ๋ฐฐ์ด์ ๋น ๊ณต๊ฐ ํ์
3๏ธโฃ ๋น ๊ณต๊ฐ์ ํ๋ ์ด์ด('X')๋ฅผ ๋์ ํ checkWin ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ํ๋ ์ด์ด๊ฐ ์น๋ฆฌํ๋ ๊ฒฝ์ฐ ํ์ธ
4๏ธโฃ ์น๋ฆฌ ๊ฐ๋ฅํ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ฉด, ํด๋น ์์น์ ์ปดํจํฐ('O')๋ฅผ ๋์ ํ๋ ์ด์ด์ ์น๋ฆฌ ๋ฐฉํด
C) ์น๋ฆฌ ๊ฐ๋ฅํ ์๊ฐ ์๊ฑฐ๋ ํ๋ ์ด์ด์ ์น๋ฆฌ๋ฅผ ๋ง์ ์ ์๋ ๊ฒฝ์ฐ์ ์ ์ ํ ์๊ณ ๋ฆฌ์ฆ
1๏ธโฃ ์ปดํจํฐ๋ ๋จ์ ๋น ๊ณต๊ฐ ์ค ๋ฌด์์๋ก ํ๋๋ฅผ ์ ํ
179p_Lab ๋์ ๋ฌธ์ )
package chap04;
import java.util.Scanner;
public class Lab_Advanced_179p {
public static void main(String[] args) {
boolean[][] board = new boolean[10][10];
int[][] mineCounts = new int[10][10];
char[][] userView = new char[10][10];
// Initialize the board with mines (true values)
// For this example, let's say we have mines at positions (2, 2), (4, 5), and (8, 9)
board[2][2] = true;
board[4][5] = true;
board[8][9] = true;
// Initialize the user view with hidden cells
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
userView[i][j] = '*';
}
}
Scanner scanner = new Scanner(System.in);
while (true) {
// Print the user view of the board
printBoard(userView);
// Ask the user to enter the cell coordinates to reveal
System.out.print("Enter row and column (e.g., 2 3) or 'q' to quit: ");
String input = scanner.nextLine().trim().toLowerCase();
if (input.equals("q")) {
System.out.println("Thanks for playing. Goodbye!");
break;
}
try {
String[] parts = input.split(" ");
int row = Integer.parseInt(parts[0]);
int col = Integer.parseInt(parts[1]);
// Check if the entered coordinates are valid
if (row < 0 || row >= 10 || col < 0 || col >= 10) {
System.out.println("Invalid coordinates. Please try again.");
continue;
}
// Check if the cell is already revealed
if (userView[row][col] != '*') {
System.out.println("Cell already revealed. Please try again.");
continue;
}
// Reveal the cell based on the board's content
if (board[row][col]) {
// The cell contains a mine
System.out.println("Oops! You hit a mine. Game Over!");
printBoard(board); // Print the board with mine positions
break;
} else {
// The cell does not contain a mine, reveal the count of neighboring mines
userView[row][col] = (char) (countNeighboringMines(board, row, col) + '0');
}
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid input format. Please enter row and column numbers separated by space.");
}
}
}
// Helper method to print the user view of the board
private static void printBoard(char[][] board) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
// Helper method to print the board with mine positions
private static void printBoard(boolean[][] board) {
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.print(board[i][j] ? "M " : ". "); // "M" for mine, "." for empty cell
}
System.out.println();
}
}
// Helper method to count neighboring mines for a cell
private static int countNeighboringMines(boolean[][] board, int x, int y) {
int count = 0;
for (int i = Math.max(0, x - 1); i <= Math.min(x + 1, 9); i++) {
for (int j = Math.max(0, y - 1); j <= Math.min(y + 1, 9); j++) {
if (board[i][j]) {
count++;
}
}
}
return count;
}
}