1. Abdelrahman Ghanem
  2. chess_queen_demo

Commits

Abdelrahman Ghanem  committed cc3934e

Init..

  • Participants
  • Branches default

Comments (0)

Files changed (3)

File dot.svg

View file
  • Ignore whitespace
Added
New image

File queen.svg

View file
  • Ignore whitespace
Added
New image

File queen_demo.rb

View file
  • Ignore whitespace
+#!/usr/bin/env ruby
+require 'rubygems'
+require 'Qt4'
+
+#ruby 1.8
+Infinity = (1.0/0.0)
+NaN = (0.0/0.0)
+
+Black = Qt::Brush.new(Qt::Color.new("black"))
+White = Qt::Brush.new(Qt::Color.new("white"))
+
+def attacked?(ax, ay, bx, by)
+  slope = (by - ay).to_f / (bx - ax)
+  return(true) if [ax, ay] == [bx, by] # slope => NaN
+  [0.0, 1.0, -1.0, -Infinity, Infinity].include? slope
+  #slope == 0.0 || slope == 1.0 || slope == -1.0 || slope == Infinity || slope == -Infinity
+end
+
+def color(row, column)
+  if row % 2 > 0 and column % 2 > 0
+    White
+  elsif row % 2 == 0 and column % 2 == 0
+    White
+  else
+    Black
+  end
+end
+
+class ChessTable < Qt::TableWidget
+  
+  def initialize(n = 8, selection = Qt::TableWidget::NoSelection, parent = nil)
+    super(n, n,  parent)
+    self.selectionMode = selection
+    horizontalHeader.setResizeMode(Qt::HeaderView::Stretch)
+    verticalHeader.setResizeMode(Qt::HeaderView::Stretch)
+    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff)
+    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff)
+    @n = n
+    loadBoard
+    setQueenAt(n/2, n/2)
+  end
+  
+  def loadBoard(n = @n)
+    @n = n
+    self.horizontalHeaderLabels = ('1'..n.to_s).to_a
+    self.verticalHeaderLabels = ('1'..n.to_s).to_a
+    
+    all = n * n
+    items = (1..all).map{|i| Qt::TableWidgetItem.new}
+    row = 0
+    column = 0
+
+    items.each_with_index do |item, index|
+      self.setItem(row, column, item)
+      item.background = color(row, column)
+      index += 1
+      column += 1
+      if index % n == 0
+        row += 1
+        column = 0
+      end
+    end
+  end
+  
+  def setQueenAt(qx = @n /2, qy = @n /2)
+    self.clear
+    loadBoard(@n)
+    qx, qy = qy, qx #row => y, column => x when moving
+    cells = (1..@n).inject([]){|a, i| a.concat((1..@n).map{|j| [i, j]})}
+    cells.select{|x1, y1| attacked?(qx, qy, x1,y1)}.each do |a , b|
+      attacked_label = Qt::Label.new
+      attacked_label.alignment = Qt::AlignCenter
+      attacked_label.pixmap = Qt::Pixmap.new('dot.svg')
+      self.setCellWidget(a-1, b-1, attacked_label)
+    end
+    queen_label = Qt::Label.new
+    queen_label.scaledContents = true
+    queen_label.pixmap = Qt::Pixmap.new('queen.svg')
+    self.setCellWidget(qx-1, qy-1, queen_label)
+  end
+  
+end
+
+
+class Window < Qt::Widget
+  slots 'on_spin_n_valueChanged(int)',
+         'on_spinQX_valueChanged(int)',
+         'on_spinQY_valueChanged(int)'
+         
+  def initialize
+    super(nil)
+    self.windowTitle = "Queen Demo"
+    self.resize(600, 500)
+    @spin_n = Qt::SpinBox.new
+    @spin_n.minimum = 2
+    @spin_n.maximum = 100
+    @spin_n.value = 8
+    @spin_n.prefix = "N: "
+    @spin_n.objectName = "spin_n"
+    
+    @spinQX = Qt::SpinBox.new
+    @spinQY = Qt::SpinBox.new
+    
+    @spinQX.minimum = 1
+    @spinQX.maximum = 100
+    @spinQX.value = 4
+    @spinQX.prefix = "X: "
+    @spinQX.objectName = "spinQX"
+    
+    @spinQY.minimum = 1
+    @spinQY.maximum = 100
+    @spinQY.value = 4
+    @spinQY.prefix = "Y: "
+    @spinQY.objectName = "spinQY"
+    
+    @hbox = Qt::HBoxLayout.new
+    @vbox = Qt::VBoxLayout.new
+    
+    @hbox.addWidget(@spin_n)
+    @hbox.addWidget(@spinQX)
+    @hbox.addWidget(@spinQY)
+    
+    @vbox.addLayout(@hbox)
+    
+    @chessTable = ChessTable.new(8, Qt::AbstractItemView::SingleSelection)
+    on_spin_n_valueChanged(8)
+    @vbox.addWidget(@chessTable)
+    
+    self.layout = @vbox
+    Qt::MetaObject.connectSlotsByName(self)
+  end
+  
+  def on_spin_n_valueChanged(n)
+    @chessTable.rowCount = n
+    @chessTable.columnCount = n
+    @chessTable.loadBoard(n)
+    @spinQX.maximum = n
+    @spinQY.maximum = n
+    qx, qy = @spinQX.value, @spinQY.value
+    @chessTable.setQueenAt(qx, qy)
+  end
+  
+  def on_spinQX_valueChanged(qx)
+    qy = @spinQY.value
+    @chessTable.setQueenAt(qx, qy)
+  end
+  
+  def on_spinQY_valueChanged(qy)
+    qx = @spinQX.value
+    @chessTable.setQueenAt(qx, qy)
+  end
+  
+end
+
+if $0 == __FILE__
+  app = Qt::Application.new(ARGV)
+  w = Window.new
+  w.show
+  app.exec
+end