Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Anyone can tell me how to put in the 0 in the array to make the tic tac toe.
I can only create X in the output but no idea how to insert 0 and X together

Thanks



JavaScript
function drawgrid {
	#########################################
	#          update display               #
	#########################################
	write-host
	$line1 = "  "+$row1[0]+" | "+$row1[1]+" | "+$row1[2]
	$line1
	$dividerline = " ----------"
	$dividerline
	$line2 = "  "+$row2[0]+" | "+$row2[1]+" | "+$row2[2]
	$line2
	$dividerline
	$line3 = "  "+$row3[0]+" | "+$row3[1]+" | "+$row3[2]
	$line3
	write-host
}
#########################################
#        setup and instructions         #
#########################################
$gameover = 'false'
$row1= @("7","8","9")
$row2 = @("4","5","6")
$row3 = @("1","2","3")
write-host
write-host "tic-tac-toe"
write-host "Enter a number (1-9) to place an X in that square"
write-host "Press Q to give up, R to restart"
drawgrid
#########################################
#               game loop               #
#########################################
while ($gameover -ne 'true') {

	#########################################
	#          get user input               #
	#########################################
	$target = Read-Host "Enter the grid number: "
	if (($target -ge 1) -and ($target -le 9)) {
	
	
##### need to add checks for R and Q inputs
		

		########################################################
		#  check that the user input points to an empty square #
		########################################################
		switch ($target) 
		{ 
        1 { if($row3[0] -eq "1") { $row3[0] = "X" } }
        2 { if($row3[1] -eq "2") { $row3[1] = "X" } }
        3 { if($row3[2] -eq "3") { $row3[2] = "X" } }
        4 { if($row2[0] -eq "4") { $row2[0] = "X" } }
        5 { if($row2[1] -eq "5") { $row2[1] = "X" } }
        6 { if($row2[2] -eq "6") { $row2[2] = "X" } }
        7 { if($row1[0] -eq "7") { $row1[0] = "X" } }
        8 { if($row1[1] -eq "8") { $row1[1] = "X" } }
        9 { if($row1[2] -eq "9") { $row1[2] = "X" } }
		}
		###########################
		#     redraw the grid     #
		###########################
		
		drawgrid
	
		
	}
	
	if ($target -eq 'Q') {
		$gameover = 'true'
	}
	
	if ($target -eq 'R') {
		$row1= @("7","8","9")
		$row2 = @("4","5","6")
		$row3 = @("1","2","3")
		drawgrid
	}
}
Posted
Updated 3-Jul-12 6:37am
v2
Comments
[no name] 3-Jul-12 12:07pm    
And the reason that you are reposting this is because...?
Richard MacCutchan 3-Jul-12 12:38pm    
After putting the user's X in a spot, you need to work out where is the best place for your 0 and add it in.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900