From e383c27a8d81e9e3851992a0a9eea62cffc4961a Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 13 Sep 2011 13:21:40 -0700 Subject: [PATCH 1/2] command line player interface. needs user move validation, ai move get, and actualy move do --- checkers.rb | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/checkers.rb b/checkers.rb index 4115f73..9d36479 100644 --- a/checkers.rb +++ b/checkers.rb @@ -59,7 +59,7 @@ class Board } str += "\n" } - str += 'TEAM_1: ' + @stats[TEAM_1]['count'].to_s + ' -- TEAM_2: ' + @stats[TEAM_2]['count'].to_s + "\n" + str += 'White: ' + @stats[TEAM_1]['count'].to_s + ' -- Black: ' + @stats[TEAM_2]['count'].to_s + "\n" return str end @@ -300,5 +300,85 @@ class Board end end end + + def parse_coords(str) + if str.length != 2 + return false + end + + y = str.downcase[0] + x = str[1] + + if x >= '0' and x <= '7' + x = x.ord - '0'.ord + else + return false + end + + if y >= 'a' and y <= 'h' + y = y.ord - 'a'.ord + else + return false + end + return [x,y] + end + + def play() + setup() + color = '' + while color != 'w' and color != 'b' + print "Choose color ([W]hite or [B]lack): " + color = gets + color = color.downcase[0] + end + + turn = 1 + team = TEAM_1 + while true + print "Turn " + turn.to_s + ": " + if team == TEAM_1 + print "white " + if color == 'w' + print "(player)" + else + print "(ai)" + end + else + print "black " + if color == 'b' + print '(player)' + else + print '(ai)' + end + end + print "\n" + puts to_s() + + if (team == TEAM_1 and color == 'w') or (team == TEAM_2 and color == 'b') + valid = false + while !valid + print "Move piece: " + from = gets + print "to: " + to = gets + + from = parse_coords(from) + to = parse_coords(to) + if !to or !from + next + end + if team(to[0], to[y]) != team + next + end + valid = true + end + else + puts "AI MOVE" + end + team = opposite_team(team) + turn += 1 + end + end + end From 5b1d69ebfed19afe118d60b8166acbabe928990e Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 13 Sep 2011 21:54:01 -0700 Subject: [PATCH 2/2] basic play interface --- checkers.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/checkers.rb b/checkers.rb index 9d36479..6ebb8a6 100644 --- a/checkers.rb +++ b/checkers.rb @@ -323,6 +323,7 @@ class Board return [x,y] end + # todo: deal with loss when no move avail def play() setup() color = '' @@ -358,23 +359,33 @@ class Board valid = false while !valid print "Move piece: " - from = gets + from = gets().strip! print "to: " - to = gets - + to = gets().strip! from = parse_coords(from) to = parse_coords(to) + puts to.to_s + " or " + from.to_s if !to or !from next end - if team(to[0], to[y]) != team + if team(from[0], from[1]) != team + next + end + if !piece_valid_moves(from[0], from[1], team).include?(to) next end valid = true end + do_move(from[0], from[1], to, team) else puts "AI MOVE" end + + if @stats[TEAM_1]['count'] == 0 + puts "Black wins!" + elsif @stats[TEAM_2]['count'] == 0 + puts "White wins!" + end team = opposite_team(team) turn += 1 end