View Full Version : سورس 8 وزیر
mojtaba_cpp
چهارشنبه 23 خرداد 1386, 20:50 عصر
#include <iostream.h>
#include<conio.h>
int test( int i , int j );
int board[8][8] = { 0 };
int main()
{
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++ )
cout << board[m][n] << " ";
cout << endl;
}
cout << endl;
getch();
}
cout << endl;
return 0;
}
int test( int i , int j )
{
for ( int a = 0 ; a < 8 ; a++ )
board[i][a] = 0;
for ( int b = 1 ; b < 8 ; b++ )
if ( i - b >= 0 ) {
if ( board[i - b][j] != 0 )
return 0;
if ( j - b >= 0 )
if ( board[i - b][j - b] != 0 )
return 0;
if ( j + b <= 7 )
if ( board[i - b][j + b] != 0 )
return 0;
}
board[i][j] = 1;
return 1;
}
daneshjo IT
پنج شنبه 24 خرداد 1386, 10:52 صبح
یک سوال داشتم:چرا این برنامه از محیط اجرایی خارج نمیشود؟
emad_67
پنج شنبه 24 خرداد 1386, 11:22 صبح
یک سوال داشتم:چرا این برنامه از محیط اجرایی خارج نمیشود؟
چرا خارج میشه چون در یه صفحه 8*8 تعداد حالات 92 تا است خوب تا 92 حالت رو نشون نده برنامه تموم نمیشه
mojtaba_cpp
پنج شنبه 24 خرداد 1386, 11:38 صبح
بله همون طور که آقا عماد گفت تا 92 حالت رو نشون نده خارج نمی شه . با زدن اینتر می تونید حالت ها رو ببینید ...
sm_hossein
دوشنبه 28 خرداد 1386, 20:30 عصر
راه های دیگری هم برای نوشتن 8 وزیر یا در کل n وزیر وجود داره. برنامه ی قبلی را نمی شه به n وزیر تعمیم داد ولی برنامه ی زیر که به صورت بازگشتی و با روش BackTrack نوشته شده رو می شه به N وزیر به راحتی تعمیم داد:
#include <iostream>
#include <stdlib.h>
#include<conio.h>
using namespace std;
#define N 8
bool mark[N];
int a[N];
void bt (int n)
{
if (n == N)
{
for (int i = 0; i < N; i++, cout << endl)
for (int j = 0; j < N; j++)
if (a[i] == j)
cout << "1 ";
else
cout << "0 ";
cout << endl;
getch ();
}
else
{
for (int i = 0; i < N; i++)
if (!mark[i])
{
bool b = true;
for (int j = 0; j < n; j++)
if (n - j == abs(i - a[j]))
b = false;
if (b)
{
mark[i] = true;
a[n] = i;
bt(n + 1);
mark[i] = false;
}
}
}
}
int main ()
{
bt(0);
}
محمدامین شریفی
جمعه 15 خرداد 1388, 20:21 عصر
غیر بازگشتی:
اکه آقای mojtaba_CPP در همین تاپیک آنرا پیشنهاد داده بودند:
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 ;
}
}
}
بازگشتی:
که آقای SChat (http://www.codeproject.com/Members/SChat) در codeproject (http://www.codeproject.com/KB/java/EightQueen.aspx) آنرا پیشنهاد داده بودند:
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;
}
}}
کد طراحی هر دوی آن به یک صورت است:
(منبع (http://www.codeproject.com/KB/game/reversi.aspx))
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 حالت ممکن در نظر گرفته شده است.
دیگر الگوریتم های ممکن را می توانید در همین فارم پیدا کنید.
terrorhell
چهارشنبه 19 بهمن 1390, 15:26 عصر
سلام لطفاً یک فایل که بدون ارور هست و برای C یا C++ است بزارید
چون کد اول ارور داشت
ممنون میشم سورس کامل بزارید
programset
دوشنبه 24 بهمن 1390, 12:51 عصر
اینم یه هشت وزیر گرافیکی که پارسال با borland کامپایل کردم
توجه کنید رسم گرافیکی توسط تابع draw انجام میشه
سورس و فایل کامپایل شده رو زیپ کردم بزارم اما گزینه ای تو سایت برا ضمیمه کردن نبود اگه خواستین ایمیل میکنم براتون
yaser.nabavi1@gmail.com
/******************************
* Queens - Program *
* Projects Using *
* C/C++ Codes *
* With borland C++ 5.2 *
* build by yaser.nabavi *
* programset in barnamenevi.org*
***********************************/
#include <conio.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int k[8][8];
int Count=0;
int v=8 , n=8 , i=0 , j=0 , state=0;
void move(int p,int q);
void remove(int i,int j);
int apply(int i,int j);
void draw();
void check();
//**///////////////////////////////////////////////////////////////////
int main()
{
system("cls");
draw();
getch();
for(i=0;i<n;i++)
for(j=0;j<n;j++)
move(i,j);
system("cls");
cout<<"Total states:"<<state<<endl;
getch();
return 0;
}
//**/////////////////////////////////////////////////////////////////
void remove(int i,int j)
{
int p,q;
k[i][j]=0;
Count--;
for(p=0;p<n;p++)
if(p!=i)
k[p][j]--;
for(p=0;p<n;p++)
if(p!=j)
k[i][p]--;
p=i+1;
q=j+1;
while(p<n && q<n)
{
k[p++][q++]--;
}
p=i-1;
q=j-1;
while(p>=0 && q>=0)
{
k[p--][q--]--;
}
p=i+1;
q=j-1;
while(p<n && q>=0)
{
k[p++][q--]--;
}
p=i-1;
q=j+1;
while(p>=0 && q<n)
{
k[p--][q++]--;
}
}
//**///////////////////////////////////////////////////////////////////
int apply(int i,int j)
{
int p,q;
k[i][j]=1;
Count++;
for(p=0;p<n;p++)
if(p!=i)
k[p][j]++;
for(p=0;p<n;p++)
if(p!=j)
k[i][p]++;
p=i+1;
q=j+1;
while(p<n && q<n)
{
k[p++][q++]++;
}
p=i-1;
q=j-1;
while(p>=0 && q>=0)
{
k[p--][q--]++;
}
p=i+1;
q=j-1;
while(p<n && q>=0)
{
k[p++][q--]++;
}
p=i-1;
q=j+1;
while(p>=0 && q<n)
{
k[p--][q++]++;
}
}
//**/////////////////////////////////////////////////////////////
void draw()
{
system("cls");
int i,j,x_start,x_size,y_start,y_size;
char* string;
char* strexit;
struct text_info screen_info;
cout<<"\n\n\n";
gettextinfo(&screen_info);
x_start=screen_info.curx;
y_start=screen_info.cury;
x_size=x_start+39;
y_size=y_start+23;
_setcursortype(_NOCURSOR);
textcolor(12);
/*draw board*/
gotoxy(x_start,y_start-1);
putch(218);
gotoxy(x_size,y_start-1);
putch(191);
gotoxy(x_start,y_size);
putch(192);
gotoxy(x_size,y_size);
putch(217);
for(i=x_start+1;i<x_size;i++){
gotoxy(i,y_start-1);
if(!(i%5))
putch(194);
else
putch(196);
gotoxy(i,y_size);
if(!(i%5))
putch(193);
else
putch(196);
}
for(j=y_start;j<y_size;j++){
gotoxy(x_start,j);
if(!(j%3)){
putch(195);
gotoxy(x_size,j);
putch(180);
}
else
{
putch(179);
gotoxy(x_size,j);
putch(179);
}
}
for(j=y_start+1;j<y_size;j++){
if(!(j%3)){
for(i=x_start+1;i<x_size;i++){
gotoxy(i,j);
if(!(i%5)){
putch(197);
}
else{
putch(196);
}
}
}
}
for(i=x_start+4;i<x_size;i=i+5){
for(j=y_start;j<y_size;j++){
gotoxy(i,j);
if(j%3)
putch(179);
}
}
/*draw queen*/
for(int p=0;p<n;p++)
{
for(int q=0;q<n;q++)
{
if(k[q][p]!=1)
{}
else{
textcolor(14);
gotoxy((p*5)+x_start+2,(q*3)+y_start);
putch(236);
gotoxy((p*5)+x_start+2,(q*3)+y_start+1);
putch(178);
}
}
}
char* strq;
strq="** 8 QUEEN **";
gotoxy(x_size+1,y_start+6);
textcolor(11);
while(strq!="\0"){
putch(*strq);
strq++;
}
string="press any key to continue!";
textcolor(10);
gotoxy(x_size+1,y_start+15);
if(state==0){
putch(7);
while(string!="\0"){
putch(*string);
string++;
Sleep(15);
}
}
else{
while(string!="/0"){
putch(*string);
string++;
}
gotoxy(x_size+1,y_start+18);
strexit="press E to Exit!";
textcolor(12);
while(strexit!="\0"){
putch(*strexit);
strexit++;
}
gotoxy(x_size+2,y_start+23);
char* str;
str="state: ";
textcolor(10);
while(str!="\0"){
putch(*str);
str++;
}
cout<<state;
}
}
//**//////////////////////////////////////////////////////////////////
void check()
{
if(Count==v)
{
state++;
draw();
int c=getch();
if(c=='e')exit(0);
}
}
//**///////////////////////////////////////////////////////////////////
void move(int p,int q)
{
apply(p,q);
check();
for(int i=p;i<n;i++)
{
for(int j=0;j<n;j++)
if(k[i][j]==0)
move(i,j);
}
remove(p,q);
}
programset
دوشنبه 24 بهمن 1390, 12:57 عصر
خب موفق شدم آپلود کنم
به هرحال اگر سوالی بود درخدمتیم البته کد رو به دقت بررسی کنید
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.