U E D R , A S I H C RSS

Eight Queen Problem/용쟁호투

왜? 이런짓을 저질럿나?

C, C++, Python, Java, Delphi등이 있었으나...제가 주로사용하는 Powerbuilder로 해보고 싶은 마음에 도전하게 되었습니다...

원리는 공격당하는 위치를 등록하여 퀸이 만들어질때 공격여부를 체크하는 방식입니다...공격안당하는 자리나올때까지 루프루프루프.....

재귀하는것이 귀찮아서 봉인되어있던 GOTO문을 사용했습니다...

디자인 무시,객체지향 무시, 문제 보고 코딩시작,수많은 삽질...전형적인 초보수준입니다...한마디로 쓰레기 입니다! T_T

파워빌더 소스(eightqueenproblem.sra)

~cpp 
$PBExportHeader$eightqueenproblem.sra
$PBExportComments$Generated Application Object
forward
global type eightqueenproblem from application
end type
global transaction sqlca
global dynamicdescriptionarea sqlda
global dynamicstagingarea sqlsa
global error error
global message message
end forward

global type eightqueenproblem from application
string appname = "eightqueenproblem"
end type
global eightqueenproblem eightqueenproblem

type variables
Integer	il_attack [8,8] , il_limit = 1, il_queen_count = 1
String	is_solution[8]
end variables

forward prototypes
public function boolean wf_create_queen ()
public subroutine wf_reset_maze ()
public function boolean wf_chack_attack (integer ai_x, integer ai_y)
end prototypes

public function boolean wf_create_queen ();Integer li_x,li_y

Randomize(0)
Do While il_queen_count <= 8
	li_x = Rand(8)
	li_y = Rand(8)
	il_limit++
	IF il_limit >= 30000 THEN
		il_queen_count = 1
		RETURN FALSE
	END IF
	If wf_chack_attack(li_x, li_y) Then CONTINUE;
	is_solution[il_queen_count] = 'Queen No : ' + String(il_queen_count) + ' / X : ' + String(li_x) + ' / Y : ' + String(li_y)
	il_queen_count++
	il_limit = 1
LOOP

RETURN TRUE
end function

public subroutine wf_reset_maze ();Integer	li_x, li_y
String	ls_temp[8]

FOR li_x = 1 TO 8
	FOR li_y = 1 TO 8
		il_attack[li_x, li_y] = 0 //공격루트 초기화
	NEXT
NEXT

is_solution = ls_temp
end subroutine

public function boolean wf_chack_attack (integer ai_x, integer ai_y);//32767
Integer	li_x, li_y

Yield()

IF il_attack[ai_x,ai_y] = 1 THEN RETURN TRUE

FOR li_x = 1 TO 8
	il_attack[li_x,ai_y] = 1 //X Line 공격루트 셋팅
NEXT

FOR li_y = 1 TO 8
	il_attack[ai_x,li_y] = 1 //Y Line 공격루트 셋팅
NEXT


li_x = ai_x;li_y = ai_y;

IF li_x > li_y THEN
	li_x = li_x + (1 - li_y)
	li_y = 1
ELSEIF li_x < li_y THEN
	li_y = li_y + (1 - li_x)
	li_x = 1
ELSE
	li_x = 1
	li_y = 1
END IF
DO WHILE li_x <= 8 AND li_y <= 8
	il_attack[li_x,li_y] = 1 //x,y에서 x+n,y+n방향으로 공격루트셋팅
	li_x++
	li_y++
LOOP

li_x = 9 - ai_x;li_y = ai_y;
IF li_x > li_y THEN
	li_x = li_x + (1 - li_y)
	li_y = 1
ELSEIF li_x < li_y THEN
	li_y = li_y + (1 - li_x)
	li_x = 1
ELSE
	li_x = 1
	li_y = 1
END IF
DO WHILE li_x <= 8 AND li_y <= 8
	il_attack[9 - li_x,li_y] = 1 //x,y에서 x-n,y+n방향으로 공격루트셋팅
	li_x++
	li_y++
LOOP

RETURN FALSE
end function

on eightqueenproblem.create
appname="eightqueenproblem"
message=create message
sqlca=create transaction
sqlda=create dynamicdescriptionarea
sqlsa=create dynamicstagingarea
error=create error
end on

on eightqueenproblem.destroy
destroy(sqlca)
destroy(sqlda)
destroy(sqlsa)
destroy(error)
destroy(message)
end on

event open;NS:
wf_reset_maze()

IF NOT wf_create_queen() THEN GOTO NS
beep(5)
MessageBox('Solution',is_solution[1] + '~r' + &
                      is_solution[2] + '~r' + &
                      is_solution[3] + '~r' + &
                      is_solution[4] + '~r' + &
                      is_solution[5] + '~r' + &
                      is_solution[6] + '~r' + &
                      is_solution[7] + '~r' + &
                      is_solution[8])

end event
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:11
Processing time 0.0118 sec