๐Ÿ“‘ํ”„๋กœ์ ํŠธ-์ž๋ฐ” ์˜ค๋ชฉ ์ œ์ž‘

์œ ๋ น๊ฐœยท2021๋…„ 11์›” 23์ผ
0

์ž๋ฐ”

๋ชฉ๋ก ๋ณด๊ธฐ
3/4
post-custom-banner


์ด ํ”„๋กœ์ ํŠธ๋Š” ๋Œ€ํ•™๊ต ๋™์•„๋ฆฌ์—์„œ ํ•™์ƒ ์ž์น˜ ์ž๋ฐ” ์ˆ˜์—… ํ›„์— ์ž๋ฐ”๋กœ ์ œ์ž‘ํ–ˆ๋˜ ๊ฐ„๋‹จํ•œ ํ”„๋กœ์ ํŠธ๋‹ค.
์• ์ดˆ์— ์–•์€ ์ž๋ฐ”์ง€์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋„ ํ•œ์ •๋˜์–ด์žˆ๊ณ , ์ž๋ฐ”๋Š” ์ฃผ๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌ๊ฐ™์€ ๋ฌต์งํ•œ ๋ฒก์—”๋“œ ์„œ๋ฒ„๋‚˜ Spring, Flutter ๋“ฑ๊ณผ ์—ฐ๊ณ„ํ•ด์„œ ์“ฐ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ• ๊นŒ ์ƒ๊ฐํ•ด๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž๋ฐ”๋กœ ์˜ค๋ชฉ์„ ๋งŒ๋“ค์–ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ฐพ์•„๋ณด๋‹ˆ ๋ฐฑ์ค€ 2615๋ฒˆ ๋ฌธ์ œ์™€ ๋งค์šฐ ํก์‚ฌํ•ด ์ด๋ฅผ ์ฐธ๊ณ ํ•˜๊ธฐ๋„ ํ–ˆ๋‹ค.
ํฐ ํ”„๋กœ์ ํŠธ๋Š” ์•„๋‹ˆ๋ฉฐ ์ž๋ฐ”๋ฅผ ์ฒ˜์Œ ์ ‘ํ•˜๊ฑฐ๋‚˜ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ด€์‹ฌ์ด ์žˆ๋Š” ์‚ฌ๋žŒ์ด ๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.


๋ฉ”์ธํด๋ž˜์Šค ์ œ์ž‘

๋จผ์ € ์ธํ„ฐํŽ˜์ด์Šค๋ถ€ํ„ฐ ๊ตฌ์ƒ์„ ํ•ด์ฃผ๊ธฐ๋กœ ํ•˜์˜€๋‹ค.
GUIํˆด์„ ์“ธ ์ƒ๊ฐ์€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํžˆ ์ฝ˜์†” ์ถœ๋ ฅ ํ˜•์‹์œผ๋กœ ์ œ์ž‘ํ•  ์˜ˆ์ •์ด์—ˆ๋‹ค.

์œ„์— ์‚ฌ์ง„์— ๋ณด๋‹ค์‹œํ”ผ ํด๋ž˜์Šค๋Š” ๋ฉ”์ธ ์ธํ„ฐํŽ˜์ด์Šค์™€ ํ™”๋ฉด์„ ๋‹ด๋‹นํ•˜๋Š” Omok_Main ํด๋ž˜์Šค์™€ ์œ„์น˜ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ธฐ์ž…ํ•  Find ํด๋ž˜์Šค๋ฅผ ๋จผ์ € ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

//๋ฐ”๋‘‘ํŒ
	static int [][] matrix = new int[10][10];
	static int count; 
	static int i;

Omok_Main ๋ถ€ํ„ฐ ์ž‘์„ฑ์„ ์‹œ์ž‘ํ•ด๋ณด์ž.
๋จผ์ € ๋ณด๋‹ค์‹œํ”ผ matrix๊ฐ™์€ ๊ฒฝ์šฐ๋Š” static์œผ๋กœ ์„ ์–ธ์„ ํ•ด์ฃผ์—ˆ๋Š”๋ฐ, ์ด๊ฒŒ ๋ฌด์Šจ ํฐ ํ”„๋กœ์ ํŠธ๋„ ์•„๋‹ ๋ฟ ๋”๋Ÿฌ ํŒจํ‚ค์ง€ ๋‚ด์— ์ด์ฐจ์› ๋ฐฐ์—ด์€ ํ•˜๋‚˜๋ฐ–์— ์—†์„ ์˜ˆ์ •์ด๊ธฐ ๋•Œ๋ฌธ์— ์ •์ ์œผ๋กœ ์„ ์–ธํ•ด๋„ ํฐ ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š์ง€๋งŒ, ๋งŒ์•ฝ ๋” ํฐ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ •์  ๋ฉ”์†Œ๋“œ๋Š” ์ข€ ๋” ์‹ ์ค‘ํžˆ ์จ์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.
๊ทธ์™ธ ํ”Œ๋ ˆ์ด์–ด 1,2๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•œ count ๋ณ€์ˆ˜์™€ i๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.

//์ถœ๋ ฅ
	void print()
	{
		for(int i=0;i<Array.getLength(matrix);i++)
		{
			System.out.print(Arrays.toString(matrix[i]));
			System.out.println("");
		}

์ด๊ฑด ๊ทธ๋ƒฅ ๋‹จ์ˆœํžˆ ๋ฐฐ์—ด ์ถœ๋ ฅ ๋ฉ”์†Œ๋“œ๋‹ค. ์„ค๋ช…ํ•  ๊ฒŒ ๋” ์žˆ์„๊นŒ.

public static void main(String args[])
	{
		Omok_Main m = new Omok_Main();
		Scanner input = new Scanner(System.in);
		count++;
		System.out.println("=============๊ฐ„๋‹จํ•œ ์˜ค๋ชฉ=============");
		System.out.println("ํ”Œ๋ ˆ์ด์–ด 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ๋ฐ”๋‘‘ํŒ์— 1์€ ํ”Œ๋ ˆ์ด์–ด1, 2๋Š” ํ”Œ๋ ˆ์ด์–ด2 ์ž…๋‹ˆ๋‹ค");
		System.out.println("๋ฐ”๋‘‘ํŒ์€ 10x10์ด๋ฉฐ ํ”Œ๋ ˆ์ด์–ด1 ๋ถ€ํ„ฐ ์ž๋™ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค");
		System.out.println("===================================");
		while(true)
		{
			Find f = new Find();
			f.win();
			if(i == 1)
			{   System.out.println("๊ฒŒ์ž„์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค");
				if(count%2==0)
				{
					System.out.print("ํ”Œ๋ ˆ์ด์–ด1์ด ์ด๊ฒผ์Šต๋‹ˆ๋‹ค");
				}
				else 
				{
					System.out.print("ํ”Œ๋ ˆ์ด์–ด2๊ฐ€ ์ด๊ฒผ์Šต๋‹ˆ๋‹ค");
				}
				break;
			}
			else if(count%2==1)
			{
				System.out.println("ํ”Œ๋ ˆ์ด์–ด1======================");
				System.out.print("๋ช‡๋ฒˆ์งธ ํ–‰์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int a = input.nextInt();
				System.out.println();
				System.out.print("๋ช‡๋ฒˆ์งธ ์—ด์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int b = input.nextInt();
				System.out.println("");
				
				//์—๋Ÿฌ๋ฐฉ์ง€
				if(m.matrix[a-1][b-1] != 0)
				{
					System.out.println("์ด๋ฏธ ๋‘” ๊ณณ์€ ๋‹ค์‹œ ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค");
					continue;
				}
			
				m.matrix[a-1][b-1] = 1 ;
				count++;
				m.print();
				System.out.println("=============================");
			}
			else if(count%2==0)
			{
				System.out.println("ํ”Œ๋ ˆ์ด์–ด2======================");
				System.out.print("๋ช‡๋ฒˆ์งธ ํ–‰์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int c = input.nextInt();
				System.out.println("");
				System.out.print("๋ช‡๋ฒˆ์งธ ์—ด์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int d = input.nextInt();
				System.out.println("");
				//์—๋Ÿฌ๋ฐฉ์ง€
				if(m.matrix[c-1][d-1] != 0)
				{
					System.out.println("์ด๋ฏธ ๋‘” ๊ณณ์€ ๋‹ค์‹œ ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!");
					continue;
				}
				
				m.matrix[c-1][d-1] = 2 ;
				count++;
				m.print();
				System.out.println("=============================");
				
			}
		}
     }

๋ฉ”์ธ ๋ฉ”์†Œ๋“œ ๊ธธ์ด๊ฐ€ ๊ต‰์žฅํžˆ ๊ธธ๋‹ค. ์†”์งํžˆ ๋ฉ˜ํ† ๋ง์—์„œ ์ƒ์„ฑ์ž๋ž‘ ์ƒ์†๊นŒ์ง€๋งŒ ๊ฒจ์šฐ ์„ค๋ช…ํ•ด์คฌ๋Š”๋ฐ, ์ฝ”๋“œ๋ฅผ ๊ตณ์ด ์ •๋ฆฌํ•  ํ•„์š”์„ฑ์ด ์žˆ๋‚˜ ์‹ถ์–ด ์ƒ๊ฐ๋‚˜๋Š”๋Œ€๋กœ ๋ง‰ ๋„์ ์˜€๋”๋‹ˆ ์ด๋ ‡๊ฒŒ ๋ฌ๋‹ค(?)....
๋จผ์ € while ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ๋Œ๋ ค์„œ ์˜ค๋ชฉ์„ ๊ณ„์† ์ง„ํ–‰ํ•˜๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค. ํ”Œ๋ ˆ์ด์–ด 1๊ณผ 2๋กœ ๋‚˜๋‰˜์–ด ์•„๊นŒ ๋งŒ๋“  count ๋ณ€์ˆ˜๋ฅผ ํ™€/์ง์œผ๋กœ ๊ตฌ๋ณ„ํ•˜์—ฌ ์‹๋ณ„๋˜๊ฒŒ๋” ์ œ์ž‘ํ•˜์˜€๋‹ค. ์ด๋ฏธ ๋‘” ๊ณณ์€ ์œ„์— ์—๋Ÿฌ๋ฐฉ์ง€๋ผ๋Š” ์ฃผ์„์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋‘” ๊ฒƒ ์ฒ˜๋Ÿผ ๋ง‰์•„๋†จ๊ณ , ํ”Œ๋ ˆ์ด์–ด 1์ด ๋‘๋ฉด 1, 2๊ฐ€ ๋‘๋ฉด 2๋กœ ํ‘œ์‹œ๋˜๊ฒŒ๋” ํ•ด๋†จ๋‹ค.

์ฝ”๋“œ์˜ ์ตœ์ƒ๋‹จ ๋ถ€๋ถ„์— ๋ณด๋ฉด Find ๊ฐ์ฒด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ ์ €๊ฒŒ ์œ„์น˜ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ถ€๋ถ„์ด๋‹ค. ์•„๊นŒ ์ •์ ์œผ๋กœ matrix๋ฅผ ์„ ์–ธํ–ˆ๋˜ ์ด์œ ์ด๊ธฐ๋„ ํ•˜๋‹ค. Find์— ๊ทธ๋Œ€๋กœ matrix ์“ฐ๋ ค๊ณ ...


์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž‘์„ฑ

์ ์–ด๋„ ๋‚ด๊ฐ€ ์•Œ๊ณ ์žˆ๋Š” ์œ„์น˜ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ DFS,BFS,A-Star,๋‹ค์ต์ŠคํŠธ๋ผ ๋“ฑ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. A-Star๋Š” ๋‚ด๊ฐ€ ์ด๋ฏธ ๋‚ด ๋ฒจ๋กœ๊ทธ์— ๊ธฐ์ˆ ํ•˜๊ธฐ๋„ ํ–ˆ์—ˆ๊ณ , ์˜ค๋Š˜์€ ๊ทธ์ค‘

DFS(Deep First Search)

์ธ ๊นŠ์ด ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ

BFS(Breadth First Search)

์ธ ๋„“์ด ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ๋‹ค๋ค„๋ณด๊ณ ์ž ํ•œ๋‹ค. ์™œ๋ƒ๊ณ ? ์˜ค๋ชฉ๋งŒ๋“œ๋Š”๋ฐ ์“ธ๊บผ๋‹ˆ๊นŒ

BFS

๊นŠ๊ณ  ์–ด๋ ค์šด ์„ค๋ช…์€ ๋ชปํ•ด์ฃผ๊ฒ ๋‹ค. ์™œ๋ƒ๋ฉด ๋‚œ ์ดˆ๋ณด๋‹ˆ๊นŒ...
๋Œ€์‹  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ณ  ์ง๊ด€์ ์œผ๋กœ ์•Œ์•„๋ณด๊ฒŒ๋” ์“ฐ๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ์ƒ๊ธด ๋…ธ๋“œ์™€ ๋ฃจํŠธ๋“ค์ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. ์ฐธ๊ณ ๋กœ ๋…ธ๋“œ๋Š” ๋™๊ทธ๋ผ๋ฏธ๊ณ  ๋ฃจํŠธ๋Š” ์„ ์ด๋‹ค.
์ด๋ฆ„๋ถ€ํ„ฐ๊ฐ€ ๋„“์ด ์šฐ์„  ํƒ์ƒ‰์ด๋‹ค. ๋ญ”๊ฐ€ ๊นŠ๊ฒŒ ๋“ค์–ด๊ฐ„๋‹ค๊ธฐ ๋ณด๋‹ค๋Š” ์ซ™ wideํ•˜๊ฒŒ ํƒ์ƒ‰ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ๋“œ๋Š” ์ด๋ฆ„์ด์ง€ ์•Š์€๊ฐ€? ๊ทผ๋ฐ ๊ทธ๊ฒŒ ๋งž๋‹ค.

์ด๋ ‡๊ฒŒ ๋ฌธ์–ด๋ฐœ์‹์œผ๋กœ ๋งˆ๊ตฌ ํƒ์ƒ‰์„ ํ•˜๋Š” ๊ฒƒ์„ BFS(Breadth First Search) ๋ผ๊ณ  ํ•œ๋‹ค.

์ด์‚ฐ๊ตฌ์กฐ์—์„œ ๋ฐฐ์› ๋Š”๋ฐ, ์ฒซ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ๋ฃจํŠธ ๋…ธ๋“œ๋ผ๊ณ  ํ•˜๊ณ  ๊ทธ ๋‹ค์Œ ์ €๋Ÿฐ ์”ฉ์œผ๋กœ ์ญ‰~ ๋ญ”๊ฐ€ ์ธต์ด ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ชจ์—ฌ์žˆ๋Š” ๋…ธ๋“œ๋“ค์˜ ์ง‘ํ•ฉ์„ Level 0์ด๋ผ๊ณ  ๋ถ€๋ฅด๊ณ  ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์•„๋ž˜๋กœ ๊ฐˆ์ˆ˜๋ก ๋ ˆ๋ฒจ1,2... ์ด๋Ÿฐ์”ฉ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๋Š๋‚Œ์œผ๋กœ ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.
๊ทธ๋Ÿผ DFS๋Š” ๋ญ๋ƒ?

DFS


๋„“์ด ์šฐ์„  ํƒ์ƒ‰๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ ๋ญ”๊ฐ€ ๊นŠ์ˆ™ํžˆ ๋“ค์–ด๊ฐ€์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋“ค์„ ๋‹ค ํ•ด์น˜์šฐ๋Š” ๋Š๋‚Œ? ์ด๊ฒŒ DFS๋‹ค.
์ง„์งœ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์ด๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋‹จ ๊นŠ์ˆ™ํžˆ ๋“ค์–ด๊ฐ€์„œ ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์˜ค๋ฅธ์ชฝ์ด๋‚˜ ์ค‘๊ฐ„๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉด ์•ˆ๋˜๋ƒ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ผ๋‹จ ๋‚ด๊ฐ€ ๋ฐฐ์šด๋Œ€๋กœ๋ผ๋ฉด ์ด์‚ฐ๊ตฌ์กฐ์—์„œ DFS์™€ BFS๋Š” ์™ผ์ชฝ์„ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰์„ ํ•˜๋Š”๊ฒŒ ๊ธฐ๋ณธ ์›์น™์ด๋‹ค. ์•„๋‹˜๋ง๊ณ .

์ž ๊ทธ๋Ÿผ ์ด์ œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์–ผ์ถ” ์ •๋ฆฌ๋ฅผ ๋๋ƒˆ์œผ๋‹ˆ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž.

์•Œ๊ณ ๋ฆฌ์ฆ˜ ํด๋ž˜์Šค ์ œ์ž‘

static final int[] dx = {-1,0,1,1};
static final int[] dy = {1,1,1,0};
static final int[] check = {-1, 5};

๋ณดํ†ต BFS์™€ DFS๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋Š” ์ƒํ•˜์ขŒ์šฐ๋กœ ํƒ์ƒ‰ํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์œ„์™€ ๊ฐ™์ด dx,dy๋ฅผ ์ด์šฉํ•ด ์ด๋™ ๋ฐฉํ–ฅ์„ ๋ฏธ๋ฆฌ ์ •ํ•ด์ฃผ๋ฉด ์ˆ˜์›”ํ•˜๋‹ค.

public void win() {
	Omok_Main m = new Omok_Main();
	int win = 0;
	int[][] ba = new int[10][10];
	
	for (int i = 0; i < ba.length; i++) {
		for (int j = 0; j < ba[0].length; j++) {
			ba[i][j] = m.matrix[i][j];
		}
	}

์—ฌ๊ธฐ์„œ ba๋Š” Findํด๋ž˜์Šค์— ํ•œ์ •๋œ ๋ฐฐ์—ด์ด๋ฉฐ, matrix์˜ ๋ณต์‚ฌ๋ณธ์ด๊ธฐ๋„ ํ•˜๋‹ค. ๋ฐ”๋กœ matrix๋กœ ๋„˜๊ฒจ๋ฒ„๋ฆฌ๋ฉด ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ์™€ ์ถฉ๋Œํ•  ์œ„ํ—˜์„ฑ ๋•Œ๋ฌธ์— ba๋ผ๋Š” ๋ฐฐ์—ด๋กœ ํ•œ๋ฒˆ ๋„˜๊ฒจ์„œ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ ์šฉ์‹œํ‚ค๋„๋ก ํ–ˆ๋‹ค.

if(ba[x][y] == 1 || ba[x][y] ==2) {
				int omok = ba[x][y];
						here:for (int k = 0; k < dx.length; k++) {
							x = i;
							y = j;
							for (int h = 0; h < 4; h++) {
								x+=dx[k];
								y+=dy[k];
								if(x >=0 && x < ba.length && y >=0 && y < ba.length) {
									if(ba[x][y] == omok) {
										continue;
									}else continue here;
								}else continue here;
							}
							

์ด ๋ถ„ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด๊ฐ€๋ฉด์„œ BFS๋ฅผ ์ ์šฉ์‹œ์ผฐ๋‹ค. ์†”์งํžˆ ๋งํ•ด์„œ DFS์™€ BFS๋ฅผ ์ ์šฉ์‹œํ‚ฌ์ˆ˜๋Š” ์žˆ๋Š”๋ฐ ๊ตณ์ด ์ ์šฉ์‹œํ‚ค๋ฉด์„œ ํ•ด๊ฐˆ ๋ฌธ์ œ๋Š” ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค. Deque๊ฐ™์€ ๊ฑธ ์“ธ ์ผ๋„ ์—†๊ณ , ๊ทธ๋ƒฅ ๋‹จ์ˆœํžˆ dx์™€ dy๋ฅผ ์„ค์ •ํ•˜๊ณ  ์ƒํ•˜์ขŒ์šฐ ํƒ์ƒ‰์„ ํ•˜๋Š” ๊ฐœ๋…์ด๋‹ˆ.
์–ด์จ‹๋“  ์ฝ”๋“œ ์ˆ˜์ •์„ ๋งˆ์น˜๊ณ  ์‹คํ–‰์„ ํ•ด๋ณด์•˜๋‹ค.


๋ณด๋‹ค์‹œํ”ผ ์ž‘๋™์ด ์•„์ฃผ ์ž˜ ๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.
ํ•œ๊ฐ€์ง€ ์˜ค์ ์ด ์žˆ๋‹ค๋ฉด ArrayIndexOutOfBoundsException์ด ๋œฐ๊บผ๊ฐ™๊ธด ํ•œ๋ฐ ๊ณ ์ž‘ ๋Œ€ํ•™๊ต 1ํ•™๋…„ ํ”„๋กœ์ ํŠธ์— ์„ค๋ช…ํ•ด์ค€๊ฑด ํด๋ž˜์Šค/์ƒ์†/์ƒ์„ฑ์ž๊ฐ€ ๊ฑฐ์˜ ์ „๋ถ€์ธ๋ฐ ๊ทธ๊ฒŒ ๋Œ€์ˆ˜์ธ๊ฐ€. ์–ผ์ถ” ๊ตฌํ˜„์‹œ์ผฐ๋‹ค๋Š” ๊ฑฐ์— ๋งŒ์กฑํ•˜๊ณ  try/catch๋Š” ์“ฐ๊ธฐ ๊ท€์ฐฎ์•„์„œ ๊ฒฐ๊ตญ ์•ˆ์ผ๋‹ค..

์•„๋ž˜์— ์ฝ”๋“œ ์ „๋ฌธ์„ ์ฒจ๋ถ€ํ•˜๋‹ˆ ์–ธ์  ๊ฐ€ ์ฐธ๊ณ ํ•  ์ผ์ด ์žˆ์œผ๋ฉด ์ข‹๊ฒ ๋‹ค! ๋!


>Find ํด๋ž˜์Šค
public class Find{

static final int[] dx = {-1,0,1,1};
static final int[] dy = {1,1,1,0};
static final int[] check = {-1, 5};


public void win() {
	Omok_Main m = new Omok_Main();
	int win = 0;
	int[][] ba = new int[10][10];
	
	for (int i = 0; i < ba.length; i++) {
		for (int j = 0; j < ba[0].length; j++) {
			ba[i][j] = m.matrix[i][j];
		}
	}

	end:for (int i = 0; i < ba.length; i++) {
		for (int j = 0; j < ba[0].length; j++) {
			int x = i;
			int y = j;
			if(ba[x][y] == 1 || ba[x][y] ==2) {
				int omok = ba[x][y];
					here:for (int k = 0; k < dx.length; k++) {
						x = i;
						y = j;
						for (int h = 0; h < 4; h++) {
							x+=dx[k];
							y+=dy[k];
							if(x >=0 && x < ba.length && y >=0 && y < ba.length) {
								if(ba[x][y] == omok) {
									continue;
								}else continue here;
							}else continue here;
						}
						
						chk:for (int k2 = 0; k2 < check.length; k2++) {
							int nx = i + check[k2]*dx[k];
							int ny = j + check[k2]*dy[k];
							if(nx >=0 && nx < ba.length && ny >=0 && ny < ba.length) {
								if(ba[nx][ny] != omok) {
									continue chk;
								}continue here;
							}
						}
						
						
						win = 1; 
						m.i = 1;
						break end; 
					}
			}	
		}
	}
	if(win != 1) m.i = 0;
			
}
}
>๋ฉ”์ธ๋ฉ”์†Œ๋“œ
import java.lang.reflect.Array;
import java.util.*;
public class Omok_Main {
	//๋ฐ”๋‘‘ํŒ
	static int [][] matrix = new int[10][10];
	static int count; 
	static int i;
	//์ถœ๋ ฅ
	void print()
	{
		for(int i=0;i<Array.getLength(matrix);i++)
		{
			System.out.print(Arrays.toString(matrix[i]));
			System.out.println("");
		}
	}
	//ํƒ์ƒ‰ 
	


	//๋ฉ”์ธ๋ฉ”์†Œ๋“œ
	public static void main(String args[])
	{
		Omok_Main m = new Omok_Main();
		Scanner input = new Scanner(System.in);
		count++;
		System.out.println("=============๊ฐ„๋‹จํ•œ ์˜ค๋ชฉ=============");
		System.out.println("ํ”Œ๋ ˆ์ด์–ด 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋ฉฐ ๋ฐ”๋‘‘ํŒ์— 1์€ ํ”Œ๋ ˆ์ด์–ด1, 2๋Š” ํ”Œ๋ ˆ์ด์–ด2 ์ž…๋‹ˆ๋‹ค");
		System.out.println("๋ฐ”๋‘‘ํŒ์€ 10x10์ด๋ฉฐ ํ”Œ๋ ˆ์ด์–ด1 ๋ถ€ํ„ฐ ์ž๋™ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค");
		System.out.println("===================================");
		while(true)
		{
			Find f = new Find();
			f.win();
			if(i == 1)
			{   System.out.println("๊ฒŒ์ž„์ด ๋๋‚ฌ์Šต๋‹ˆ๋‹ค");
				if(count%2==0)
				{
					System.out.print("ํ”Œ๋ ˆ์ด์–ด1์ด ์ด๊ฒผ์Šต๋‹ˆ๋‹ค");
				}
				else 
				{
					System.out.print("ํ”Œ๋ ˆ์ด์–ด2๊ฐ€ ์ด๊ฒผ์Šต๋‹ˆ๋‹ค");
				}
				break;
			}
			else if(count%2==1)
			{
				System.out.println("ํ”Œ๋ ˆ์ด์–ด1======================");
				System.out.print("๋ช‡๋ฒˆ์งธ ํ–‰์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int a = input.nextInt();
				System.out.println();
				System.out.print("๋ช‡๋ฒˆ์งธ ์—ด์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int b = input.nextInt();
				System.out.println("");
				
				//์—๋Ÿฌ๋ฐฉ์ง€
				if(m.matrix[a-1][b-1] != 0)
				{
					System.out.println("์ด๋ฏธ ๋‘” ๊ณณ์€ ๋‹ค์‹œ ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค");
					continue;
				}
			
				m.matrix[a-1][b-1] = 1 ;
				count++;
				m.print();
				System.out.println("=============================");
			}
			else if(count%2==0)
			{
				System.out.println("ํ”Œ๋ ˆ์ด์–ด2======================");
				System.out.print("๋ช‡๋ฒˆ์งธ ํ–‰์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int c = input.nextInt();
				System.out.println("");
				System.out.print("๋ช‡๋ฒˆ์งธ ์—ด์— ๋‘์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?: ");
				int d = input.nextInt();
				System.out.println("");
				//์—๋Ÿฌ๋ฐฉ์ง€
				if(m.matrix[c-1][d-1] != 0)
				{
					System.out.println("์ด๋ฏธ ๋‘” ๊ณณ์€ ๋‹ค์‹œ ๋‘˜ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!");
					continue;
				}
				
				m.matrix[c-1][d-1] = 2 ;
				count++;
				m.print();
				System.out.println("=============================");
				
			}
		}
		
	}
}
profile
ํ•œ๋ฆผ๋Œ€ํ•™๊ต ์ •๋ณด๊ณผํ•™๋Œ€ 2ํ•™๋…„ ์žฌํ•™์ค‘ / ์œก๊ตฐ ์ •๋ณด๋ณดํ˜ธ๋ณ‘ 22-2๊ธฐ
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€