๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Java Programming/์–ด์„œ์™€ Java๋Š” ์ฒ˜์Œ์ด์ง€!_2019 Ver

[์–ด์„œ์™€ Java๋Š” ์ฒ˜์Œ์ด์ง€!_2019 Ver] 4์žฅ Lab ๋„์ „๋ฌธ์ œ ํ’€์ด

by A Lim Han 2023. 6. 25.

# 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) โ€ป

ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’๊ณผ ๋น„๊ตํ•˜๋ฉด์„œ ํƒ์ƒ‰ ๋ฒ”์œ„๋ฅผ ์ขํ˜€๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ,
์ฃผ๋กœ ๋ฐฐ์—ด์ด ์ •๋ ฌ๋œ ์ƒํƒœ์—์„œ ์‚ฌ์šฉ๋œ๋‹ค.

 

+ ์ด๋ฏธ์ง€ ์ถœ์ฒ˜

https://ko.wikipedia.org/wiki/%EC%9D%B4%EC%A7%84_%EA%B2%80%EC%83%89_%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98

 

์ด์ง„ ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „

์œ„ํ‚ค๋ฐฑ๊ณผ, ์šฐ๋ฆฌ ๋ชจ๋‘์˜ ๋ฐฑ๊ณผ์‚ฌ์ „. ์ด์ง„ ๊ฒ€์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜(binary search algorithm)์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ •ํ•œ ๊ฐ’์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ฒ˜์Œ ์ค‘๊ฐ„์˜ ๊ฐ’์„ ์ž„์˜์˜ ๊ฐ’์œผ๋กœ ์„ ํƒํ•˜์—ฌ,

ko.wikipedia.org

 


 

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;
    }
}