نمايش پست تنها
قديمي ۰۹-۱۹-۱۳۸۹, ۰۴:۲۷ بعد از ظهر   #25 (لینک دائم)
Astaraki Female
Administrator
 
آواتار Astaraki
 
تاريخ عضويت: خرداد ۱۳۸۷
محل سكونت: تهران-کرج!
پست ها: 3,465
تشكرها: 754
16,337 تشكر در 3,127 پست
My Mood: Mehrabon
ارسال پيغام Yahoo به Astaraki
Red face

غیر بازگشتی:

كد:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace reversi
{
classeightQueen {
privateBoolean [,] board;
privateBoolean[, ,] memory;
privatestaticInt32 BoardNO = 0;
 
publicBoolean[, ,] calculate()
{
board = newBoolean [8,8];
memory = newBoolean[8, 8, 92];
 
for ( int a = 0 ; a < 8 ; a++ )
if(test( 0 , a ))
for ( int b = 0 ; b < 8 ; b++ )
if(test( 1 , b ))
for ( int c = 0 ; c < 8 ; c++ )
if(test( 2 , c ))
for ( int d = 0 ; d < 8 ; d++ )
if(test( 3 , d ))
for ( int e = 0 ; e < 8 ; e++ )
if(test( 4 , e ))
for ( int f = 0 ; f < 8 ; f++ )
if(test( 5 , f ))
for ( int g = 0 ; g < 8 ; g++ )
if(test( 6 , g ))
for ( int h = 0 ; h < 8 ; h++ )
if (test(7, h))
{
 
for ( int m = 0 ; m < 8 ; m++ ) {
for (int n = 0; n < 8; n++)
memory[m, n, BoardNO] = board[m,n];
}
BoardNO++;
}
return memory;
}
Boolean test( int i , int j )
{
for ( int a = 0 ; a < 8 ; a++ )
board[i, a] = false;
for ( int b = 1 ; b < 8 ; b++ )
if ( i - b >= 0 ) {
if ( board[i - b,j] != false )
returnfalse;
if ( j - b >= 0 )
if (board[i - b, j - b] != false)
returnfalse;
if ( j + b <= 7 )
if (board[i - b, j + b] != false)
returnfalse ;
}
board[i,j]= true ;
returntrue ;
}
}
}

بازگشتی: ( که در اين لينک هم قرار داده شده)

كد:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
//calculate
namespace reversi
{
classeightQueen{
privateint [] board;
privateBoolean[,,] memory;
privateint number=0;
 
/**
* Each element of the board[] represents a row and the value of each element
* specifies the column number where the queen is placed on that particular row.
* This method is used to retrieve the board array.
*
* @return
*/
publicBoolean [,,] getBoard() {
return memory;
}
public eightQueen()
{
 
board = newint[8];
memory = newBoolean[8, 8, 93];
this.place(0);
}
/**
* Place a queen in a row and check its validity. If valid then place it in the
* next row else backtrack.
*
* @param row
*/
publicvoid place(int row) {
if (row == 8) {
 
for (int r = 0; r< 8; r++)
{
for (int c = 0; c < 8; c++)
{
if (board[r] == c){
memory[r, c, number] = true;
// MessageBox.Show(r.ToString()+"/"+c.ToString()+"/"+number.ToString());
}else
memory[r, c, number] = false;
}
}
this.number++;
return;
} else {
for (int i = 0; i < 8; i++) {
board[row] = i;
if (valid(row)) place(row + 1);
}
}
}
/**
* Checks the validity of a position. If valid returns true else returns false.
*
* @param row
* @return
*/
publicBoolean valid(int row) {
for (int i = 0; i < row; i++) {
if ((board[i] == board[row]) || Math.Abs(board[row] - board[i]) == (row - i)) {
returnfalse;
}
}
returntrue;
}
}}


کد طراحی هر دوی آن به یک صورت است:

كد:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace reversi
{
publicpartialclassReversi : Form
{
private System.Windows.Forms.Panel squaresPanel;
private System.Windows.Forms.Panel boardPanel;
private System.Windows.Forms.Label cornerLabel;
privateLabel[] colLabels, rowLabels;
privateSquareControl[,] squareControls;
// For converting column numbers to letters and vice versa.
privatestaticString alpha = "ABCDEFGH";
// The game board.
privateBoard board;
 
privateeightQueen queen;
privateBoolean[, ,] memory;
privateInt32 moveNumber;
// Used to track which player made the last move.
// Defines a thread for running the computer move look ahead.
 
public Reversi()
{
InitializeComponent();
// Create the game board.
this.board = newBoard();
 
#region bord designer
// Create the controls for each square, add them to the squares
// panel and set up event handling.
this.squareControls = newSquareControl[8, 8];
int i, j;
for (i = 0; i < 8; i++)
for (j = 0; j < 8; j++)
{
// Create it.
this.squareControls[i, j] = newSquareControl(i, j);
// Position it.
this.squareControls[i, j].Left = j * this.squareControls[i, j].Width;
this.squareControls[i, j].Top = i * this.squareControls[i, j].Height;
// Add it.
this.squaresPanel.Controls.Add(this.squareControls[i, j]);
// Set up event handling for it.
this.squareControls[i, j].Click += newEventHandler(this.SquareControl_Click);
 
}
// Create the column and row labels.
this.colLabels = newLabel[8];
for (i = 0; i < 8; i++)
{
// Create a column label.
this.colLabels[i] = newLabel();
// Set its display properties.
this.colLabels[i].Text = Reversi.alpha.Substring(i, 1);
this.colLabels[i].BackColor = this.cornerLabel.BackColor;
this.colLabels[i].ForeColor = this.cornerLabel.ForeColor;
this.colLabels[i].TextAlign = ContentAlignment.MiddleCenter;
// Set its size and position.
this.colLabels[i].Width = this.squareControls[0, 0].Width;
this.colLabels[i].Height = this.cornerLabel.Height;
this.colLabels[i].Left = this.cornerLabel.Width + i * this.colLabels[0].Width;
this.colLabels[i].Top = 0;
// Add it.
this.boardPanel.Controls.Add(this.colLabels[i]);
}
this.rowLabels = newLabel[8];
for (i = 0; i < 8; i++)
{
// Create a row label.
this.rowLabels[i] = newLabel();
// Set its display properties.
this.rowLabels[i].Text = (i + 1).ToString();
this.rowLabels[i].BackColor = this.cornerLabel.BackColor;
this.rowLabels[i].ForeColor = this.cornerLabel.ForeColor;
this.rowLabels[i].TextAlign = ContentAlignment.MiddleCenter;
// Set its size and position.
this.rowLabels[i].Width = this.cornerLabel.Height;
this.rowLabels[i].Height = this.squareControls[0, 0].Height;
this.rowLabels[i].Left = 0;
this.rowLabels[i].Top = this.cornerLabel.Height + i * this.rowLabels[0].Width * 2;
 
// Add it.
this.boardPanel.Controls.Add(this.rowLabels[i]);
}
#endregion
// Initialize the game state.
 
queen = neweightQueen();
memory = queen.getBoard();
}
privatevoid UpdateBoardDisplay()
{
if (moveNumber < 93&&moveNumber>-1)
{
this.moveNumber++;
// Map the current game board to the square controls.
SquareControl squareControl;
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
squareControl = (SquareControl)this.squaresPanel.Controls[row * 8 + col];
if (memory[row, col, this.moveNumber] == true)
{
 
squareControl.Contents = 1;
}
else squareControl.Contents = 0;
}
}
// Redraw the board.
this.squaresPanel.Refresh();
}
}
privatevoid SquareControl_Click(object sender, System.EventArgs e)
{
 
this.UpdateBoardDisplay();
}
}
}

استفاده از این برنامه به عنوان پروژه درسی مجاز نیست.دوستان تنها مجاز به استفاده از قسمت گرافیک این برنامه هستند.
برای پیاده سازی دیگر الگوریتم ها تنها کافیست که یک آرایه 3 بعدی را با الگوریتم تان برگردانید.
اندیس اول و دوم این آرایه برای ردیف و ستون صفحه شطرنج در نظر گرفته شده است،و اندیس سوم هم برای ذخیره 92 حالت ممکن در نظر گرفته شده است.

دیگر الگوریتم های ممکن را می توانید در همین فارم پیدا کنید.
فايل ضميمه
نوع فايل: zip non_Recessive_eight_queen.zip (63.9 كيلو بايت, 374 نمايش)
نوع فايل: zip Recessive_eight_queen.zip (59.6 كيلو بايت, 18 نمايش)

ويرايش شده توسط Astaraki; ۰۹-۱۹-۱۳۸۹ در ساعت ۰۴:۲۹ بعد از ظهر
Astaraki آفلاين است   پاسخ با نقل قول