1 Replies - 469 Views - Last Post: 02 October 2011 - 02:21 PM Rate Topic: -----

#1 dev_JC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 30-September 11

i'm coding a tictactoe game, So how do you determine who goes next

Posted 02 October 2011 - 04:23 AM

I'm Coding a tictactoe game its not yet finish, so I'm having a BIG problem here, how do determine who is supposed to do the move, I know the user goes first but do I make the computer move. I dont have the code yet to change the choosen box to become 'X' or "O'. Because there is a need to know who is making the turn so that I can tell what symbol to print and who has occupied it already, if you have any idea on how the algorith looks like, pls teach me,.thank you so much in advance.!




%include "asm_io.inc"

;macros box occupation

%define PLAYER 0
%define COMP 1
%define UNOCCUPIED 2

;macros for Game state
%define ONGOING 0
%define DRAWN 1
%define PLAYER_WON 2
%define COMP_WON 3

segment .data
	space	db	" ", 0
	space_2	db	"  ", 0
	space_6	db	"      ", 0

	line_h	db	"______", 0
	line_v	db	"|", 0

	print_X	db	"X",0
	print_O	db	"O",0		

	prompt	db	"Choose a number where you want to put your symbol: ", 0
	prompt1 db "Hello!! THIS IS TICTACTOE:", 0
	prompt2	db "Enter 1 to start a new game: ", 0

	wrong	db "OOOooops... Wrong Input!", 0
	player1 db "PLAYER 1",0
	player2 db "PLAYER 2",0	
	win	db " You win!!", 0

	array_easy dd 0, 1, 2, 3, 4, 5, 6, 7, 8

	gs_groups dd    0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 3, 6, 1, 4, 7, 2, 5, 8, 0, 4, 8, 2, 4, 6

segment .bss
	
	player	resd	0
	turns	resd	1		;number of turns made	limit is 9 then go back to 1	

	change_char	resd 	1

	play resd 1
	array1 resd 9
	box   resd 1
	width  resd 1
	height resd 1
	size	resd 1
	size_plus resd 1 
	line	resd 1
	count_array resd 1
	choice_num resd 1

segment .text
	global asm_main
asm_main:

	enter 0,0
	pusha
	
	call	print_nl
	mov	eax,prompt1
	call	print_string
	call	print_nl

again:

	mov	eax,prompt2
	call	print_string

	call	read_int
	mov	[play], eax

	cmp	dword [play], 1
	je	play_now

	mov	eax, wrong
	call	print_string
	call 	print_nl
	jmp	again

play_now:

	mov	dword [size], 3
	jmp	print_table
	mov	dword [turns],0		;set number of turns to 0


;--------------------------PRINT TABLE--------------------------------

print_table:           
	mov	dword [box], 1		;may isa pa lng ka box
	mov	dword [line], 0		;may isa pa lng ka linya
	mov	esi,[size]		;ang size sng is 3
	add	esi,1			;add 1 para magdamo
	mov     ebx,0			;for array index     	

loop_body:
	call	print_nl
	mov	eax, space_6
	call	print_string
		    
	mov	ecx, [size]
	mov	edx, 1

	cmp	dword [box], esi	;if == jumps to asking of input
	je	call_prompt_move	

seq:
	cmp	dword [line], 0		
	je	line_0			;proceed to printing of line 1

	cmp	dword [line], 1
	je	line_1

	cmp	dword [line], 2
	je	line_2
	      
	cmp	dword [line], 3
	je	line_3
call_prompt_move:
	call	prompt_move
	jmp	loop_body
line_0:
      mov	eax, space
      call	print_string
      mov	eax, line_h
      call	print_string
      
      loop	line_0
      mov	dword [line], 1
      jmp	loop_body

line_1:
	mov	eax, line_v
	call	print_string
	mov	eax, space_6
	call	print_string
	      
	loop	line_1
	mov	eax, line_v
	call	print_string
	mov	dword [line], 2
	jmp	loop_body

line_2:      
	mov	eax, line_v
	call	print_string
	mov	eax, space_2
	call	print_string
	mov	eax, space
	call	print_string	

	;cmp	dword [change_char],20
	;je	put_X
	;cmp	dword [change_char],21
	;je	put_O

	call	pick_array
	call	print_int

line_2_cont:
	mov	eax, space_2
	call	print_string
	loop	line_2
	mov	dword [line], 3
	mov	eax, line_v
	call	print_string
	jmp	loop_body

;put_X:
;	mov	eax,print_X
;	call	print_string
;	sub	dword [change_char], 1 
;	add	ebx,1
;	jmp	line_2_cont
;put_O:	

;	mov	eax,print_O
;	call	print_string
;	add	dword [change_char], 1 
;	add	ebx,1
;	jmp	line_2_cont

line_3:
	mov	eax, line_v
	call	print_string
	mov	eax, line_h
	call	print_string

	loop	line_3
	mov	dword [line], 1
	inc	dword [box]
	mov	eax, line_v
	call	print_string
	jmp	loop_body

pick_array:
	mov	eax, [array_easy + 4 * ebx]
	add	ebx,1
	ret
;-------------------------------------------------------------------------;
;------------------------------GAME STATE-------------------------------------------;
segment .data
;       { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, /* horizontal */
;        { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, /* vertical */
;        { 0, 4, 8 }, { 2, 4, 6 } /* diagonals */
;gs_groups dd    0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 3, 6, 1, 4, 7, 2, 5, 8, 0, 4, 8, 2, 4, 6

segment	.bss


segment .text
   global game_state

game_state:

	mov     esi, gs_groups
	mov	edx, 0
	mov     ecx, 8

gs_combo_loop:
;j + 2i
                   
	mov     eax, [0 + 3*edx]         
	cmp     eax, UNOCCUPIED             
	jz      gs_combo_loop_continue

	mov     eax, [1 + 3*edx]         
   	cmp     eax, UNOCCUPIED  
   	jnz     gs_combo_loop_continue

	mov     eax, [2 + 3*edx]
	cmp     eax, UNOCCUPIED
	jnz     gs_combo_loop_continue
                                       ;dapat ma satisfy nya na ang conditions
                                       ;para mabalan na may winner
                                       ; we have a winner!

                                       ;if (board[combinations[i][0]] == PLAYER
                                       ;  && board[combinations[i][1]] == PLAYER
                                       ;  && board[combinations[i][2]] == PLAYER) {
                                       ;return PLAYER_WON;

                                       ;else if (board[combinations[i][0]] == COMP
                                       ;  && board[combinations[i][1]] == COMP
                                       ;  && board[combinations[i][2]] == COMP) {
                                       ;return COMP_WON;

   cmp     eax, PLAYER
   jnz     gs_determine_winner_comp
   mov     eax, PLAYER_WON             ;eax = 2
   jmp     gs_epilog                   ;go to ret

gs_determine_winner_comp:
   mov     eax, COMP_WON               ;eax = 3
   jmp     gs_epilog                   ;goto ret

gs_combo_loop_continue:
   add     edx,1
   loop    gs_combo_loop               ; re check for next array element


; drawn or ongoing

   cld
   mov     eax, UNOCCUPIED
   mov     ecx, 9
   repne scasd

   jnz     gs_not_found
   mov     eax, ONGOING                ;set value of EAX = 2, kay wala pa may nagdaug, so continue gyapun
   jmp     gs_epilog

gs_not_found:

   mov     eax, DRAWN

gs_epilog:

   ret



;*-------------------------------------------PROMPT MOVE----------------------------------------------------*
segment .data
pm_prompt  	 db  	"Your move: ", 0
pm_prompt_err  	 db  	"Enter the number of an empty square.",0
pm_format  	 db  	"%d", 0

segment	.bss

pm_player_symbol	resd	1
pm_comp_symbol		resd	1
pm_move			resd	1

segment .text
    global prompt_move

prompt_move:
    jmp     input_loop
    
pm_input_loop_err:

    mov	    eax,pm_prompt_err
    call    print_string
    
input_loop:
    
	mov	eax,pm_prompt
	call    print_string
	call    read_int

	mov	dword [pm_move],eax
	mov	ebx,eax

pm_input_loop_check_range:
	
    	cmp     eax, 9
    	jg      pm_input_loop_err
	cmp     dword [pm_move], 1
	jl      pm_input_loop_err

    	jmp     pm_input_loop_out

pm_input_loop_out:
	
	mov	dword [box], 1		;may isa pa lng ka box
	mov	dword [line], 0		;may isa pa lng ka linya
	mov	dword [size], 3
	mov	esi,[size]		;ang size sng is 3

	mov     eax, pm_move

    ret

;*--------------------------------------------------------------------------;


ask_choice:
	
	mov	eax, dword [turns]
	add	dword [turns],1		
	mov	ecx,2			; eax = eax % 2;	;for change of player or computer will input	
	cdq
	idiv	eax	
	mov	eax, edx	
	mov	dword [player], eax
	cmp	dword [player], 0
	
	
	jne	COMP_MOVE
	
	
	call 	print_nl
	mov	eax, prompt
	call 	print_string
	call 	read_int
	mov  	dword [choice_num], eax
	mov 	ebx, 0

check_number:

	cmp	dword [choice_num], 1
	je	easy_1
	
	cmp	dword [choice_num], 2
	je	easy_2


easy_1:

	; THIS IS THE POINT WHERE WE NEED TO STORE THE USERS INPUT TO A VARIABLE , PARA MA COMPARE TA SA WINNING COMBINATIONS

	jmp	point_player1	
	inc	ebx

easy_2:

	; THIS IS THE POINT WHERE WE NEED TO STORE THE USERS INPUT TO A VARIABLE , PARA MA COMPARE TA SA WINNING COMBINATIONS
	jmp	point_player1	
	inc	ebx

point_player1:	
		mov	dword [change_char], 20
		jmp	print_table
point_PC:
	;

COMP_MOVE:
exit:
	popa
	mov eax,0
	leave
	ret





Is This A Good Question/Topic? 0
  • +

Replies To: i'm coding a tictactoe game, So how do you determine who goes next

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: i'm coding a tictactoe game, So how do you determine who goes next

Posted 02 October 2011 - 02:21 PM

code the equivalent of a do...while loop:

do {
player_moves;
computer_moves;
} while (no_winner and squares used less than 9);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1