ShubhanshuBansod commited on
Commit
6aefcf0
·
verified ·
1 Parent(s): c86fe22

Update connect_four_game.py

Browse files
Files changed (1) hide show
  1. connect_four_game.py +14 -14
connect_four_game.py CHANGED
@@ -111,27 +111,27 @@ class ConnectFour:
111
  return score
112
 
113
  def board_to_string(self) -> str:
114
- '''Aligned board display with column numbers centered above each column'''
115
  mapping = {0: '⚪', 1: '🔴', 2: '🟡'}
116
 
117
- # Choose a cell width (characters). Larger width helps compensate for emoji display width.
118
- cell_w = 4
119
 
120
- # Column header: center each column number inside the cell width
121
- header = " " * 5 # left padding for row labels
122
- header += "".join(f"{str(i):^{cell_w}}" for i in range(self.cols)) + "\n\n"
 
 
123
 
124
- # Rows: prepend a row label (0..rows-1) then each cell centered inside cell_w
125
- rows_str = ""
126
  for row in range(self.rows):
127
- rows_str += f" {row:<2} " # row label with a little padding
128
  for col in range(self.cols):
129
- # center the token in the cell width; emojis are treated as single chars,
130
- # but extra space in cell_w helps visual alignment.
131
- rows_str += f"{mapping[self.board[row][col]]:^{cell_w}}"
132
- rows_str += "\n"
133
 
134
- return header + rows_str
 
135
 
136
 
137
  def reset(self):
 
111
  return score
112
 
113
  def board_to_string(self) -> str:
114
+ '''HTML-rendered board with perfectly aligned columns'''
115
  mapping = {0: '⚪', 1: '🔴', 2: '🟡'}
116
 
117
+ # Start HTML table
118
+ html = "<table style='border-collapse: collapse; margin: auto; text-align: center;'>"
119
 
120
+ # Column headers
121
+ html += "<tr><td></td>" # Empty corner cell for row labels
122
+ for i in range(self.cols):
123
+ html += f"<th style='padding:6px; color:#FFD700; font-weight:bold;'>{i}</th>"
124
+ html += "</tr>"
125
 
126
+ # Rows
 
127
  for row in range(self.rows):
128
+ html += f"<tr><th style='padding:6px; color:#FFD700; font-weight:bold;'>{row}</th>"
129
  for col in range(self.cols):
130
+ html += f"<td style='padding:6px; font-size:26px;'>{mapping[self.board[row][col]]}</td>"
131
+ html += "</tr>"
 
 
132
 
133
+ html += "</table>"
134
+ return html
135
 
136
 
137
  def reset(self):