CREATE PROCEDURE sp_kaishi(@type CHAR(1))
AS
BEGIN
IF OBJECT_ID('wzq') IS NOT NULL
DROP TABLE wzq
CREATE TABLE wzq
(
id INT,
[1] CHAR(1) DEFAULT '',
[2] CHAR(1) DEFAULT '',
[3] CHAR(1) DEFAULT '',
[4] CHAR(1) DEFAULT '',
[5] CHAR(1) DEFAULT '',
[6] CHAR(1) DEFAULT '',
[7] CHAR(1) DEFAULT '',
[8] CHAR(1) DEFAULT '',
[9] CHAR(1) DEFAULT '',
[10] CHAR(1) DEFAULT '',
[11] CHAR(1) DEFAULT '',
[12] CHAR(1) DEFAULT '',
[13] CHAR(1) DEFAULT '',
[14] CHAR(1) DEFAULT '',
[15] CHAR(1) DEFAULT ''
)
INSERT INTO wzq(id)
SELECT number FROM master..spt_values WHERE type='P' AND number BETWEEN 1 AND 15
IF @type='x'
UPDATE wzq SET [8]='x' WHERE id=8
SELECT * FROM wzq
END
GO
CREATE PROCEDURE sp_wanjia(@I int,@J int)
AS
BEGIN
DECLARE @SQL NVARCHAR(1000)
DECLARE @R CHAR(1)
DECLARE @rst VARCHAR(10)
IF EXISTS(SELECT 1 FROM wzq WHERE id=16)
BEGIN
SELECT '游戏已经结束,请重新开始' AS 温馨提示
GOTO lab
END
IF @I NOT BETWEEN 1 AND 15
BEGIN
SELECT '请输入1到15的行' AS 温馨提示
GOTO lab
END
IF @J NOT BETWEEN 1 AND 15
BEGIN
SELECT '请输入1到15的列' AS 温馨提示
GOTO lab
END
EXEC sp_getvalue 'wzq',@I,@J,@R OUTPUT
IF @R<>''
BEGIN
SELECT '不允许重复走子' AS 温馨提示
GOTO lab
END
SET @SQL='UPDATE wzq SET ['+convert(Varchar(10),@J)+']=''0'' Where ID='+Convert(Varchar(10),@I)
EXEC sp_executesql @SQL
EXEC sp_shuying '0',@rst OUTPUT
IF @rst IS NOT NULL
BEGIN
SELECT @rst AS 温馨提示
INSERT INTO wzq(id) VALUES(16)
END
ELSE
BEGIN
EXEC sp_computerDown @i OUT,@j OUT
SET @SQL='UPDATE wzq SET ['+convert(Varchar(10),@J)+']=''X'' Where ID='+Convert(Varchar(10),@I)
EXEC sp_executesql @SQL
EXEC sp_shuying 'x',@rst OUTPUT
IF @rst IS NOT NULL
BEGIN
SELECT @rst AS 温馨提示
INSERT INTO wzq(id) VALUES(16)
END
END
lab:
SELECT * FROM dbo.wzq
ORDER BY id
SET @SQL='UPDATE wzq SET ['+convert(Varchar(10),@J)+']=''x'' Where ID='+Convert(Varchar(10),@I) + ' And ['+ convert(Varchar(10),@J)+']=''X'''
EXEC sp_executesql @SQL
END
GO
CREATE PROCEDURE sp_shuying(@type CHAR(1),@rst varchar(10) OUTPUT)
AS
BEGIN
DECLARE @i INT=1
DECLARE @j INT=1
DECLARE @x INT
DECLARE @y INT
DECLARE @r1 CHAR(1),@r2 CHAR(1),@r3 CHAR(1),@r4 CHAR(1),@r5 CHAR(1)
SET @rst=NULL
WHILE @i<=15
BEGIN
SET @j=1
WHILE @j<=15
BEGIN
--横向
SET @x =@i
EXEC sp_getvalue 'wzq',@x,@j,@r1 OUTPUT
SET @x=@x+1
EXEC sp_getvalue 'wzq',@x,@j,@r2 OUTPUT
SET @x=@x+1
EXEC sp_getvalue 'wzq',@x,@j,@r3 OUTPUT
SET @x=@x+1
EXEC sp_getvalue 'wzq',@x,@j,@r4 OUTPUT
SET @x=@x+1
EXEC sp_getvalue 'wzq',@x,@j,@r5 OUTPUT
IF @r1= @type and @i <= 15 - 4 AND @r1 = @r2 and @r1 = @r3 AND @r1 = @r4 and @r1 = @r5
BEGIN
SET @rst = '{xx}赢了1'
END
--纵向
SET @y=@j
EXEC sp_getvalue 'wzq',@i,@y,@r1 OUTPUT
SET @y=@y+1
EXEC sp_getvalue 'wzq',@i,@y,@r2 OUTPUT
SET @y=@y+1
EXEC sp_getvalue 'wzq',@i,@y,@r3 OUTPUT
SET @y=@y+1
EXEC sp_getvalue 'wzq',@i,@y,@r4 OUTPUT
SET @y=@y+1
EXEC sp_getvalue 'wzq',@i,@y,@r5 OUTPUT
IF @r1= @type and @j <= 15 - 4 AND @r1 = @r2 and @r1 = @r3 AND @r1 = @r4 and @r1 = @r5
BEGIN
SET @rst = '{xx}赢了2'
END
SET @x=@i
SET @y=@j
EXEC sp_getvalue 'wzq',@x,@y,@r1 OUTPUT
SET @x=@x+1
SET @y=@y+1
EXEC sp_getvalue 'wzq',@x,@y,@r2 OUTPUT
SET @x=@x+1
SET @y=@y+1
EXEC sp_getvalue 'wzq',@x,@y,@r3 OUTPUT
SET @x=@x+1
SET @y=@y+1
EXEC sp_getvalue 'wzq',@x,@y,@r4 OUTPUT
SET @x=@x+1
SET @y=@y+1
EXEC sp_getvalue 'wzq',@x,@y,@r5 OUTPUT
--正斜线
IF @r1= @type AND @i <= 15 - 4 and @j <= 15 - 4 AND @r1 = @r2 and @r1 = @r3 AND @r1 = @r4 and @r1 = @r5
BEGIN
SET @rst = '{xx}赢了3'
END
SET @j=@j+1
END
SET @i=@i+1
END
--反斜线
SET @i=1
SET @j=1
WHILE @i<=15
BEGIN
SET @j=15
WHILE @j>=5
BEGIN
SET @x=@i
SET @y=@j
EXEC sp_getvalue 'wzq',@x,@y,@r1 OUTPUT
SET @x=@x+1
SET @y=@y-1
EXEC sp_getvalue 'wzq',@x,@y,@r2 OUTPUT
SET @x=@x+1
SET @y=@y-1
EXEC sp_getvalue 'wzq',@x,@y,@r3 OUTPUT
SET @x=@x+1
SET @y=@y-1
EXEC sp_getvalue 'wzq',@x,@y,@r4 OUTPUT
SET @x=@x+1
SET @y=@y-1
EXEC sp_getvalue 'wzq',@x,@y,@r5 OUTPUT
IF @r1= @type AND @r1 = @r2 and @r1 = @r3 AND @r1 = @r4 and @r1 = @r5
BEGIN
SET @rst = '{xx}赢了4'
END
SET @j=@j-1
END
SET @i=@i+1
END
IF @type='0'
SET @rst=REPLACE(@rst,'{xx}','您')
ELSE
SET @rst=REPLACE(@rst,'{xx}','数据库')
END
GO
CREATE PROCEDURE sp_getvalue(@tabname VARCHAR(200),@I INT,@J INT,@val VARCHAR(20) OUTPUT)
AS
BEGIN
DECLARE @SQL NVARCHAR(1000)
SET @val=''
IF NOT EXISTS(SELECT 1 FROM sys.columns WHERE object_id=OBJECT_ID(@tabname) AND name=CONVERT(VARCHAR(10),@J))
BEGIN
RETURN
END
Set @SQL='Select @val=['+convert(Varchar(10),@J)+'] From '+QUOTENAME(@tabname)+' Where ID='+Convert(Varchar(10),@I)
EXEC sp_executesql @SQL,N'@val VARCHAR(20) out',@val OUT
SET @val=ISNULL(@val,'')
END
go
CREATE PROCEDURE sp_computerDown(@x int OUTPUT,@y int OUTPUT)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [wuzhiqi].[StoredProcedures].[sp_computerDown]
GO
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void sp_computerDown(out int x,out int y )
{
x = 1;
y = 1;
using (SqlConnection cn = new SqlConnection("context connection=true"))
{
SqlCommand cmd = new SqlCommand("select [1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15] from wzq order by id", cn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
computerDown(dataTable, out x, out y);
}
}
private static int chessScore(int playerNum, int computerNum)
{
if (playerNum > 4)
{
return 400000;
}
if (playerNum > 0 && computerNum > 0)
{
return 0;
}
if (playerNum == 0 && computerNum == 0)
{
return 14;
}
if (playerNum == 1)
{
return 30;
}
if (playerNum == 2)
{
return 800;
}
if (playerNum == 3)
{
return 3600;
}
if (playerNum == 4)
{
return 200000;
}
if (computerNum == 1)
{
return 70;
}
if (computerNum == 2)
{
return 1600;
}
if (computerNum == 3)
{
return 30000;
}
if (computerNum == 4)
{
return 1600000;
}
return -1;
}
private static void computerDown(DataTable dt,out int x, out int y)
{
x = 0;
y = 0;
int chessWidth = 15;
int[,] score = new int[15, 15];
string[,] chessPlace = new string[15, 15];
// 初始化
for (int i = 0; i < chessWidth; i++)
{
for (int j = 0; j < chessWidth; j++)
{
score[i, j] = 0;
}
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
chessPlace[i, j] = dt.Rows[i][j].ToString().Trim();
}
}
int playerNum = 0;
int computerNum = 0;
int tempScore = 0;
int maxScore = -1;
// 横向寻找
for (int i = 0; i < chessWidth; i++)
{
for (int j = 0; j < chessWidth - 4; j++)
{
for (int k = j; k < j + 5; k++)
{
if (chessPlace[k, i] == "0")
{
playerNum++;
}
else if (chessPlace[k, i] == "x")
{
computerNum++;
}
if (k + 1 < chessWidth)
{
if (playerNum == 3 && chessPlace[k + 1, i] == "0")
{
playerNum++;
}
}
}
tempScore = chessScore(playerNum, computerNum);
// 为该五元组的每个位置添加分数
for (int k = j; k < j + 5; k++)
{
score[k, i] += tempScore;
}
playerNum = 0;
computerNum = 0;
tempScore = 0;
}
}
// 纵向寻找
for (int i = 0; i < chessWidth; i++)
{
for (int j = 0; j < chessWidth - 4; j++)
{
for (int k = 0; k < j + 5; k++)
{
if (chessPlace[i, k] == "0")
{
playerNum++;
}
else if (chessPlace[i, k] == "x")
{
computerNum++;
}
if (k + 1 < chessWidth)
{
if (playerNum == 3 && chessPlace[i, k + 1] == "0")
{
playerNum++;
}
}
}
tempScore = chessScore(playerNum, computerNum);
for (int k = j; k < j + 5; k++)
{
score[i, k] += tempScore;
}
playerNum = 0;
computerNum = 0;
tempScore = 0;
}
}
// 反斜线上侧部分
for (int i = chessWidth - 1; i >= 4; i--)
{
for (int k = i, j = 0; j < chessWidth && k >= 0; j++, k--)
{
int m = k; //x 14 13
int n = j; //y 0 1
for (; m > k - 5 && k - 5 >= -1; m--, n++)
{
if (chessPlace[m, n] == "0")
{
playerNum++;
}
else if (chessPlace[m, n] == "x")
{
computerNum++;
}
if (m - 1 > 0 && n + 1 < chessWidth)
{
if (playerNum == 3 && chessPlace[m - 1, n + 1] == "0")
{
playerNum++;
}
}
}
if (m == k - 5)
{
tempScore = chessScore(playerNum, computerNum);
for (m = k, n = j; m > k - 5; m--, n++)
{
score[m, n] += tempScore;
}
}
playerNum = 0;
computerNum = 0;
tempScore = 0;
}
}
// 反斜线下侧部分
for (int i = 1; i < 15; i++)
{
for (int k = i, j = chessWidth - 1; j >= 0 && k < 15; j--, k++)
{
int m = k; //y 1
int n = j; //x 14
for (; m < k + 5 && k + 5 <= 15; m++, n--)
{
if (chessPlace[n, m] == "0")
{
playerNum++;
}
else if (chessPlace[n, m] == "x")
{
computerNum++;
}
if (m + 1 < chessWidth && n - 1 > 0)
{
if (playerNum == 3 && chessPlace[n - 1, m + 1] == "0")
{
playerNum++;
}
}
}
if (m == k + 5)
{
tempScore = chessScore(playerNum, computerNum);
for (m = k, n = j; m < k + 5; m++, n--)
{
score[n, m] += tempScore;
}
}
playerNum = 0;
computerNum = 0;
tempScore = 0;
}
}
// 正斜线上侧部分
for (int i = 0; i < chessWidth - 1; i++)
{
for (int k = i, j = 0; j < chessWidth && k < chessWidth; j++, k++)
{
int m = k;
int n = j;
for (; m < k + 5 && k + 5 <= chessWidth; m++, n++)
{
if (chessPlace[m, n] == "0")
{
playerNum++;
}
else if (chessPlace[m, n] == "x")
{
computerNum++;
}
if (m + 1 < chessWidth && n + 1 < chessWidth)
{
if (playerNum == 3 && chessPlace[m + 1, n + 1] == "0")
{
playerNum++;
}
}
}
if (m == k + 5)
{
tempScore = chessScore(playerNum, computerNum);
for (m = k, n = j; m < k + 5; m++, n++)
{
score[m, n] += tempScore;
}
}
playerNum = 0;
computerNum = 0;
tempScore = 0;
}
}
// 正斜线下侧部分
for (int i = 1; i < chessWidth - 4; i++)
{
for (int k = i, j = 0; j < chessWidth && k < chessWidth; j++, k++)
{
int m = k;
int n = j;
for (; m < k + 5 && k + 5 <= chessWidth; m++, n++)
{
if (chessPlace[n, m] == "0")
{
playerNum++;
}
else if (chessPlace[n, m] == "x")
{
computerNum++;
}
if (m + 1 < chessWidth && n + 1 < chessWidth)
{
if (playerNum == 3 && chessPlace[n + 1, m + 1] == "0")
{
playerNum++;
}
}
}
if (m == k + 5)
{
tempScore = chessScore(playerNum, computerNum);
for (m = k, n = j; m < k + 5; m++, n++)
{
score[n, m] += tempScore;
}
}
playerNum = 0;
computerNum = 0;
tempScore = 0;
}
}
// 从空位置中找到得分最大的位置
for (int i = 0; i < chessWidth; i++)
{
for (int j = 0; j < chessWidth; j++)
{
if (chessPlace[i, j] == "" && score[i, j] > maxScore)
{
x = i;
y = j;
maxScore = score[i, j];
}
}
}
x++;
y++;
}
}
![](https://img-blog.csdnimg.cn/dcf3995f0f334bea909fa583eae73fe2.png)