<html>
<HEAD></HEAD>
<BODY>
<a href="0.html">-0-</a>
<a href="1.html">-1-</a>
<a href="2.html">-2-</a>
<a href="3.html">-3-</a>
<a href="4.html">-4-</a>
<a href="5.html">-5-</a>
<a href="6.html">-6-</a>
<a href="7.html">-7-</a>
<a href="8.html">-8-</a>
<a href="9.html">-9-</a>
<textarea rows="50" cols="50">
/***************
2048 目前带向上h和向左的bug
随机数
程序运行时判断相同数
刷屏
程序运行环境 vs2012或linux
**************/
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[5][5]={0};
void show()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
void fun(int state)
{
int i=0,j=0,k;
for( i=0;i<5;i++)
{
for( j=0;j<4;j++)
{
if (a[i][j]==a[i][j+1])
{
a[i][j]=2*a[i][j];
a[i][j+1]=0;
}
}
}
if(state==1)
{
for(i=0;i<5;i++)
{
for( j=4;j>0;j--)
{
if (a[i][j]!=0)continue;
else {k=j-1;
while(a[i][k]==0){ k--; }
if(k>=0){ a[i][j]=a[i][k];a[i][k]=0;}
}
}
}
}
}
void fundown(int state)
{
int i=0,j=0,k;
for( j=0;j<5;j++)
{
for( i=0;i<4;i++)
{
if (a[i][j]==a[i+1][j])
{
a[i][j]=2*a[i][j];
a[i+1][j]=0;
}
}
}
if(state==1)
{
for(j=0;j<5;j++)
{
for(i=4;i>0;i--)
{
if (a[i][j]!=0)continue;
else {k=i-1;
while(a[k][j]==0){ k--; }
if(k>=0){ a[i][j]=a[k][j];a[k][j]=0;}
}
}
}
}
}
int check()
{int s=0,i,j;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i][j]==2048){cout<<"good"<<endl;}
if(a[i][j]==4096){cout<<"good"<<endl;}
if(a[i][j]==0){s=1;}
}
}
if(!s){cout<<" no empty";exit(0);}
return s;
}
void randnum()
{
srand(time(0));
int r=rand()%5;
if(a[0][r]==0)a[0][r]=2;
else if (a[4][r]!=0) a[4][r]=2;
else {
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
a[i][j]=2;
}
}
void play()
{
char ss=‘c‘;
cout<<" enter sxzc"<<endl;
cin>>ss;
switch(ss)
{
case ‘s‘:fundown(2);break;
case ‘x‘:fundown(1);break;
case ‘z‘:fun(2);break;
case ‘c‘:fun(1);break;
}
}
int main()
{ int st=1;
while(st)
{
randnum();
show();
play();
st=check();
system("clear");
}
return 0;
}
int main1()
{
show();
int state=1;
fundown(state);
show();
return 0;
}
</textarea>
<textarea rows="50" cols="50">
bug
在于上与左 方向的产生
移动数据实现第一步
即 20200 向右移动会是 02020的结果??
所以还更应该优化这个代码
</textarea>
<textarea rows="50" cols="50">
gcc -o main main.c fun.h fun.c
ls -lh
d+G(shift+g)
10dd
10yy
:/int a
</textarea>
<textarea rows="50" cols="50">
http://blog.csdn.net/fred1653/article/details/9322583/
http://www.2cto.com/os/201508/431266.html
http://wenku.baidu.com/view/4fbb197527284b73f242509f.html
文件结束标志 eof 还有ueof是完全不同的
</textarea>
<textarea rows="50" cols="50">
/**
基数排序
偏移量版本
桶容量版本
**/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[]={1,3,5,7,9,2,4,6,8,11,13,15,17,22,24,26,28};
int len=sizeof(a)/sizeof(a[0]);
void show()
{int i=0;
while(i<len)cout<<a[i]<<" ";cout<<endl;
}
void sort(int a[])
{
int b[3][10]={0};
//MAX
int max=10;
int i=0,j=1;
for(i=0;i<len;i++)
{ while(a[i]>max)
{
max=max*10;
j++;
}
}
// 按位模除处理
max=10;
for(i=0;i<j;i++)
{
int n=len-1;int kk=0;
while(n--)
{
int xx=a[n]%max;
//放入
while(b[kk][xx]!=0 )
{
kk++;
}
b[kk][xx]=a[n];
kk=0;
}
//转回原来的位置
//循环结束自然数组a就排好了
max=max*10;
}
}
</textarea>
<textarea rows="50" cols="50"> </textarea>
<textarea rows="50" cols="50"> </textarea>
<textarea rows="50" cols="50"> </textarea>
<textarea rows="50" cols="50"> </textarea>
<textarea rows="50" cols="50"> </textarea>
</BODY>
</html>原文地址:http://wzsts.blog.51cto.com/10251779/1786748