go语言写个小游戏
时间: 2023-07-07 10:18:01 浏览: 97
好的,这里提供一个简单的猜数字游戏示例:
```go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
targetNum := rand.Intn(100) + 1
fmt.Println("猜数字游戏开始!")
for {
var guessNum int
fmt.Print("请输入一个1到100的数字:")
fmt.Scanln(&guessNum)
if guessNum == targetNum {
fmt.Println("恭喜你猜对了!")
break
} else if guessNum < targetNum {
fmt.Println("猜错了,--;
}
// 删除所有与 value 值相同的节点
int erase(Double_sentinel_link list, Type value) {
int count = 0;
Double_node* current_node = list->list_head.next_node;
while (current_node != list->list_tail) {
if (current_node->node_value == value) {
current_node->previous_node->next_node = current_node->next_node;
current_node->next_node->previous_node = current_node->previous_node;
Double_node* node_to_delete = current_node;
current_node = current_node->next_node;
free(node_to_delete);
list->list_size--;
count++;
} else {
current_node = current_node->next_node;
}
}
return count;
}
// 测试代码
int main() {
Double_sentinel_link list = create_double_list();
push_back(list, 1);
push_back(list, 2);
push_back(list, 3);
push_front(list, 0);
printf("size: %d\n", size(list));
printf("empty: %d\n", empty(list));
printf("front: %d\n", front(list));
printf("back: %d\n", back(list));
printf("begin: %d\n", begin(list)->node_value);
printf("end: %d\n", end(list)->node_value);
printf("rbegin: %d\n", rbegin(list)->node_value);
printf("rend: %d\n", rend(list)->node_value);
Double_node* node = find(list, 2);
if (node != end(list)) {
printf("find: %d\n", node->node_value);
} else {
printf("not found\n");
}
printf("count: %d\n", count(list, 2));
erase(list, 2);
printf("size: %d\n", size(list));
while (!empty(list)) {
printf("%d ", front(list));
pop_front(list);
}
printf("\n");
destroy_double_list(list);
return 0;
}
```
希望能对你有所帮助!
阅读全文