<commit_before>'use strict'

# eventually Piece should have a canMove
class Piece
	constructor: (@isWhite) ->
		@color = if @isWhite then 'white' else 'black'
		@symbol = if @isWhite then @whiteSymbol else @blackSymbol
		@hasMoved = false

	type: ->
		@name

	pointValue: ->
		@value

class BlankPiece extends Piece
	name: ""
	value: 0
	blackSymbol: "\u25A0"
	whiteSymbol: "\u25A1"

	getMoves: (x,y,board) ->
		return [[x,y]]

class Pawn extends Piece
	name: "Pawn"
	value: 1
	blackSymbol: "\u265F"
	whiteSymbol: "\u2659"

	getMoves: (x,y,game) ->
		# TODO: Implement en passant and pawn conversion.
		moves = []
		# Constant that represents which direction pawns move in.
		movement = 1
		if @color == 'black'
			movement = -1

		# Default move.
		if y+movement < 8 and game.get( x, y+movement ) instanceof BlankPiece
			moves.push( [x,y+movement] )
			# Double step.
			if not @hasMoved
				if y+(2*movement) < 8 and game.get( x, y+(2*movement) ) instanceof BlankPiece
					moves.push( [x,y+(2*movement)] )
		# Check if the pawn can capture a piece.
		if game.inBounds( x-1, y+movement )
			enemy = game.get( x-1, y+movement )
			if not (enemy instanceof BlankPiece)
				if @color != enemy.color
					moves.push( [x-1, y+movement] )
		if game.inBounds( x+1, y+movement )
			enemy = game.get( x+1, y+movement )
			if not (enemy instanceof BlankPiece)
				if @color != enemy.color
					moves.push( [x+1, y+movement] )

		# DEBUG
		i = 0
		while i < moves.length
			console.log( moves[i] )
			i++
		return moves

class Knight extends Piece
	name: "Knight"
	value: 3
	blackSymbol: "\u265E"
	whiteSymbol: "\u2658"

	getMoves: (x,y,game) ->
		moves = []
		# loops over entire board, less efficient but easier to read
		# x and y for board
		for yb in [0..7]
			for xb in [0..7]
				unless game.get(xb,yb).isWhite == @isWhite
					# x and y offsets
					xo = Math.abs (x - xb)
					yo = Math.abs (y - yb)
					if (xo == 1 && yo == 2) || (yo == 1 && xo == 2)
						moves.push( [xb,yb] )

		return moves

class Bishop extends Piece
	name: "Bishop"
	value: 3
	blackSymbol: "\u265D"
	whiteSymbol: "\u2657"

class Rook extends Piece
	name: "Rook"
	value: 5
	blackSymbol: "\u265C"
	whiteSymbol: "\u2656"

class Queen extends Piece
	name: "Queen"
	value: 9
	blackSymbol: "\u265B"
	whiteSymbol: "\u2655"

class King extends Piece
	name: "King"
	value: 137
	blackSymbol: "\u265A"
	whiteSymbol: "\u2654"



# a8 to [1,8]
anToxy = (an) ->

	x = an[0].charCodeAt 0
	x = x - 96

	y = an[1]

	return [x,y]

# 1, 8 to a8
xyToan = (x,y) ->
	return (String.fromCharCode (x+96)) + y

pieceFromChar = (c) ->
	switch c
		when "p" then new Pawn   false
		when "r" then new Rook   false
		when "n" then new Knight false
		when "b" then new Bishop false
		when "q" then new Queen  false
		when "k" then new King   false
		when "P" then new Pawn   true
		when "R" then new Rook   true
		when "N" then new Knight true
		when "B" then new Bishop true
		when "Q" then new Queen  true
		when "K" then new King   true
		else new BlankPiece



# 0,0 on board is from white's perspective.
class Game
	# fill in with peices
	constructor: ->
		@board =
		for y in [0..7]
			for x in [0..7]
				# if x+y % 2 == 0 then it's a white square
				new BlankPiece (x + y) % 2 == 0

	# these work with either algebraic notation or x,y coordinates
	set: (x, y, to) ->
		# if we need to convert a char into a piece for placement
		unless to instanceof Piece
			to = pieceFromChar(to)

		@board[x][y] = to

	get: (x, y) ->
		return @board[x][y]

	# move 1 to 2
	move: (x1, y1, x2, y2) ->
		# TODO capture logic / points

		# move the piece
		@set(x2, y2, @get(x1,y1))

		# clear the piece's old position
		@set(x1, y1, new BlankPiece)

	inBounds: (x, y) ->
		return (x >= 0 and y >= 0) and (x < 8 and y < 8)

	# fills an 8x8 board array
	visual: ->
		for y in [0..7]
			for x in [0..7]
				@get(x,y).symbol

	# draw the board to the console
	log: ->
		console.log "Current Board:"
		vis = @visual()
		for piece in vis
			console.log piece.join(' ')
<commit_msg>removed repetitive algebraic conversion, added move method<commit_after>'use strict'

# eventually Piece should have a canMove
class Piece
	constructor: (@isWhite) ->
		@color = if @isWhite then 'white' else 'black'
		@symbol = if @isWhite then @whiteSymbol else @blackSymbol
		@hasMoved = false

	type: ->
		@name

	pointValue: ->
		@value

class BlankPiece extends Piece
	name: ""
	value: 0
	blackSymbol: "\u25A0"
	whiteSymbol: "\u25A1"

	getMoves: (x,y,board) ->
		return [[x,y]]

class Pawn extends Piece
	name: "Pawn"
	value: 1
	blackSymbol: "\u265F"
	whiteSymbol: "\u2659"

	getMoves: (x,y,game) ->
		# TODO: Implement en passant and pawn conversion.
		moves = []
		# Constant that represents which direction pawns move in.
		movement = 1
		if @color == 'black'
			movement = -1

		# Default move.
		if y+movement < 8 and game.get( x, y+movement ) instanceof BlankPiece
			moves.push( [x,y+movement] )
			# Double step.
			if not @hasMoved
				if y+(2*movement) < 8 and game.get( x, y+(2*movement) ) instanceof BlankPiece
					moves.push( [x,y+(2*movement)] )
		# Check if the pawn can capture a piece.
		if game.inBounds( x-1, y+movement )
			enemy = game.get( x-1, y+movement )
			if not (enemy instanceof BlankPiece)
				if @color != enemy.color
					moves.push( [x-1, y+movement] )
		if game.inBounds( x+1, y+movement )
			enemy = game.get( x+1, y+movement )
			if not (enemy instanceof BlankPiece)
				if @color != enemy.color
					moves.push( [x+1, y+movement] )

		# DEBUG
		i = 0
		while i < moves.length
			console.log( moves[i] )
			i++
		return moves

class Knight extends Piece
	name: "Knight"
	value: 3
	blackSymbol: "\u265E"
	whiteSymbol: "\u2658"

	getMoves: (x,y,game) ->
		moves = []
		# loops over entire board, less efficient but easier to read
		# x and y for board
		for yb in [0..7]
			for xb in [0..7]
				unless game.get(xb,yb).isWhite == @isWhite
					# x and y offsets
					xo = Math.abs (x - xb)
					yo = Math.abs (y - yb)
					if (xo == 1 && yo == 2) || (yo == 1 && xo == 2)
						moves.push( [xb,yb] )

		return moves

class Bishop extends Piece
	name: "Bishop"
	value: 3
	blackSymbol: "\u265D"
	whiteSymbol: "\u2657"

class Rook extends Piece
	name: "Rook"
	value: 5
	blackSymbol: "\u265C"
	whiteSymbol: "\u2656"

class Queen extends Piece
	name: "Queen"
	value: 9
	blackSymbol: "\u265B"
	whiteSymbol: "\u2655"

class King extends Piece
	name: "King"
	value: 137
	blackSymbol: "\u265A"
	whiteSymbol: "\u2654"



# a8 to [1,8]
anToxy = (an) ->

	x = an[0].charCodeAt 0
	x = x - 96

	y = an[1]

	return [x,y]

# 1, 8 to a8
xyToan = (x,y) ->
	return (String.fromCharCode (x+96)) + y

pieceFromChar = (c) ->
	switch c
		when "p" then new Pawn   false
		when "r" then new Rook   false
		when "n" then new Knight false
		when "b" then new Bishop false
		when "q" then new Queen  false
		when "k" then new King   false
		when "P" then new Pawn   true
		when "R" then new Rook   true
		when "N" then new Knight true
		when "B" then new Bishop true
		when "Q" then new Queen  true
		when "K" then new King   true
		else new BlankPiece



# 0,0 on board is from white's perspective.
class Game
	# fill in with peices
	constructor: ->
		@board =
		for y in [0..7]
			for x in [0..7]
				# if x+y % 2 == 0 then it's a white square
				new BlankPiece (x + y) % 2 == 0

	# these work with either algebraic notation or x,y coordinates
	set: (x, y, to) ->
		# if we need to convert a char into a piece for placement
		unless to instanceof Piece
			to = pieceFromChar(to)

		@board[x][y] = to

	get: (x, y) ->
		return @board[x][y]

	# move 1 to 2
	move: (x1, y1, x2, y2) ->
		# TODO capture logic / points

		# move the piece and set flags
		p = @get(x1, y1)
		p.hasMoved = true
		@set(x2, y2, p)

		# clear the piece's old position
		@set(x1, y1, new BlankPiece)

	inBounds: (x, y) ->
		return (x >= 0 and y >= 0) and (x < 8 and y < 8)

	# fills an 8x8 board array
	visual: ->
		for y in [0..7]
			for x in [0..7]
				@get(x,y).symbol

	# draw the board to the console
	log: ->
		console.log "Current Board:"
		vis = @visual()
		for piece in vis
			console.log piece.join(' ')

{'fraction_non_alphanumeric': 0.1311960542540074, 'fraction_numerical': 0.030579531442663378, 'mean_word_length': 2.8223374175306315, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '99226', 'n_tokens_mistral': 3640, 'n_tokens_neox': 3148, 'n_words': 1094}